Skip to content

Commit f08f634

Browse files
committed
Testing and Debugging section
1 parent 1a81d68 commit f08f634

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,50 @@ The `lazy()` method is invaluable when dealing with patterns that include variab
976976

977977
By making quantifiers lazy, EloquentRegex empowers you to write more precise and effective patterns, ensuring that your matches are exactly as intended, no more and no less.
978978

979+
# Testing and Debugging Your Regex Patterns
980+
981+
Crafting the perfect regex pattern is an art that often requires iteration, testing, and debugging. EloquentRegex provides tools that make this process easier and more intuitive, allowing you to refine your patterns until they match precisely what you need. One of the most useful methods for this purpose is `toRegex()`, which outputs the raw regex pattern. Combined with online tools like [Regexr](https://regexr.com/), you can visualize and debug your patterns in a user-friendly environment.
982+
983+
#### Using the "toRegex" Method
984+
985+
The `toRegex()` method converts your EloquentRegex pattern into a standard regex pattern string. This is particularly useful for debugging purposes or when you need to share your pattern with others who might not be using EloquentRegex.
986+
987+
**Example: Converting an EloquentRegex Custom Pattern to Raw Regex**
988+
989+
```php
990+
$pattern = EloquentRegex::source('your test string here')
991+
->start()
992+
->wordChars()->whitespace()->digits()
993+
->end()
994+
->toRegex();
995+
996+
// Now, $pattern contains the raw regex string that you can test or debug further.
997+
```
998+
999+
_Note: `toRegex` doesn't return regex patterns used by the Options_
1000+
1001+
#### Debugging with Regexr
1002+
1003+
[Regexr](https://regexr.com/) is a free online tool that allows you to test and debug regex patterns in real-time. It provides a detailed explanation of each part of your regex, matches highlighted in the text, and even a reference guide for regex syntax.
1004+
1005+
##### How to Use Regexr for Debugging:
1006+
1007+
1. **Convert Your Pattern:** Use the toRegex() method to convert your EloquentRegex pattern into a raw regex string.
1008+
2. **Open Regexr:** Go to https://regexr.com/ in your web browser.
1009+
3. **Paste Your Pattern:** Paste the raw regex string into the "Expression" field on Regexr.
1010+
4. **Test Your Pattern:** Enter test strings in the "Test String" area to see how your pattern matches. Regexr will highlight matches and provide useful insights and errors if the pattern doesn't work as expected.
1011+
1012+
_Note: For debuging `get` method, open "Flags" dropdown and mark "global"_
1013+
1014+
#### Tips for Effective Debugging
1015+
1016+
- **Start Simple:** Begin with a simplified version of your pattern and gradually add complexity. This helps isolate issues.
1017+
- **Use Descriptive Test Strings:** Include a variety of test strings that cover all the scenarios you expect your pattern to handle, as well as edge cases.
1018+
- **Pay Attention to Details:** Regex patterns are sensitive to every character. Double-check your symbols, especially those that have special meanings in regex, like `.`, `*`, `?`, etc.
1019+
- **Iterate and Refine:** Don't expect to get everything right on the first try. Use the feedback from testing to refine your pattern iteratively.
1020+
1021+
Testing and debugging are critical steps in ensuring your regex patterns do exactly what you intend. By leveraging the `toRegex()` method and tools like Regexr, you can make this process more manageable and efficient, leading to more accurate and reliable regex implementations in your Laravel applications.
1022+
9791023
##### To Do
9801024

9811025
- Return captured groups while using `group()` method with `get()`.✔️
@@ -990,6 +1034,7 @@ By making quantifiers lazy, EloquentRegex empowers you to write more precise and
9901034
- Return collection on get method if laravel is available.
9911035
- Add builderPattern methods list MD file and link from the Docs.
9921036
- Implement usage of named groups: `/(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})/`
1037+
- Add some tool for debuging options
9931038

9941039
- Write documentation (add credit for https://regexr.com/ and ChatGPT)
9951040
- Create quick start guide and add in Docs.
@@ -1007,7 +1052,7 @@ By making quantifiers lazy, EloquentRegex empowers you to write more precise and
10071052
- Raw methods ✔️
10081053
- Add section in docs for "lazy" method ✔️
10091054
- Add sections:
1010-
- Testing and Debugging
1055+
- Testing and Debugging ✔️
10111056
- Credits
10121057
- Contributing (+STRUCTURE.md)
10131058
- FAQs

0 commit comments

Comments
 (0)