You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+46-1Lines changed: 46 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -976,6 +976,50 @@ The `lazy()` method is invaluable when dealing with patterns that include variab
976
976
977
977
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.
978
978
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
+
979
1023
##### To Do
980
1024
981
1025
- 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
990
1034
- Return collection on get method if laravel is available.
991
1035
- Add builderPattern methods list MD file and link from the Docs.
992
1036
- Implement usage of named groups: `/(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})/`
1037
+
- Add some tool for debuging options
993
1038
994
1039
- Write documentation (add credit for https://regexr.com/ and ChatGPT)
995
1040
- Create quick start guide and add in Docs.
@@ -1007,7 +1052,7 @@ By making quantifiers lazy, EloquentRegex empowers you to write more precise and
0 commit comments