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
@@ -905,7 +907,74 @@ In this example, we precisely match "alt=" using the `exact` method. We then cre
905
907
906
908
The `orPattern` method also accepts a quantifier as its **second argument** (after callback), applying the same [quantifier logic](#quantifier-values) as elsewhere in EloquentRegex. This feature adds another layer of flexibility, allowing you to specify how many times either pattern should be present.
907
909
908
-
---
910
+
## Raw Methods 🧩
911
+
912
+
When working with regular expressions, there are times you'll need to insert a segment of raw regex directly into your pattern. This might be due to the complexity of the pattern or simply because you're integrating an existing regex snippet. EloquentRegex accommodates this need with specific methods designed to seamlessly integrate raw regex patterns into your larger expressions.
913
+
914
+
#### Adding Raw Regex Patterns
915
+
916
+
The `addRawRegex` method allows you to insert any raw regex directly into your pattern. This is particularly useful for incorporating standard regex snippets without modification.
917
+
918
+
**Example: Matching a Social Security Number (SSN)**
919
+
920
+
```php
921
+
// Directly adds a raw regex pattern for an SSN
922
+
EloquentRegex::start('123-45-6789')
923
+
->addRawRegex('\d{3}-\d{2}-\d{4}')
924
+
->check();
925
+
// Expected to match an SSN format '123-45-6789', but not '123456789'
926
+
```
927
+
928
+
This method is straightforward and ensures that your EloquentRegex pattern can accommodate complex requirements with ease.
929
+
930
+
#### Wrapping Raw Regex in a Non-Capturing Group
931
+
932
+
Sometimes, you may want to include a raw regex snippet as part of a larger pattern without capturing its match. The `addRawNonCapturingGroup` method wraps the provided raw regex in a non-capturing group, allowing it to participate in the match without affecting the captured groups.
933
+
934
+
**Example: Adding Digits Followed by a Specific Letter**
935
+
936
+
```php
937
+
// Wraps digits in a non-capturing group and expects an 'A' immediately after
In the world of regular expressions, greediness refers to the tendency of quantifiers to match as much of the input as possible. However, there are scenarios where you want your pattern to match the smallest possible part of the input that satisfies the pattern, a behavior known as "laziness" or "non-greediness". EloquentRegex introduces a straightforward way to apply this concept through the `lazy()` method.
947
+
948
+
#### How the Lazy Method Works
949
+
950
+
The `lazy()` method modifies the behavior of quantifiers that follow it in the pattern, making them match as few characters as possible. This is particularly useful when you want to extract specific segments from a larger block of text without capturing unnecessary parts.
951
+
952
+
**Example: Extracting "Secret Coded" Messages from Text**
953
+
Consider a situation where you need to extract coded messages enclosed in curly braces and preceded by a specific keyword within a larger text. Using the greedy approach might lead to capturing more text than intended, including text between messages. The `lazy()` method ensures that only the content directly within the braces, following the keyword, is matched.
954
+
955
+
```php
956
+
$text = "Normal text {secret: message one} more text {secret: another hidden text} end";
957
+
$matches = EloquentRegex::source($text)
958
+
->lookBehind(function ($pattern) {
959
+
$pattern->openCurlyBrace()->exact('secret: ');
960
+
})
961
+
->lazy()->anyChars()
962
+
->lookAhead(function ($pattern) {
963
+
$pattern->closeCurlyBrace();
964
+
})
965
+
->get();
966
+
967
+
// Expected to extract ['message one', 'another hidden text'] as separate matches
968
+
969
+
```
970
+
971
+
In this example, without the `lazy()` method, the pattern might greedily match from the first `{secret: ` to the last `}`, including everything in between as a single match (`message one} more text {secret: another hidden text`). By applying `lazy()`, the pattern instead matches the smallest possible string that satisfies the pattern within each set of curly braces, effectively separating the messages.
972
+
973
+
#### When to Use the Lazy Method
974
+
975
+
The `lazy()` method is invaluable when dealing with patterns that include variable-length content, such as strings or blocks of text, where you aim to extract specific, bounded segments. It's especially useful in parsing structured formats embedded within free text, extracting data from templated content, or any scenario where precision is key to separating multiple matches in a larger string.
976
+
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.
909
978
910
979
##### To Do
911
980
@@ -935,8 +1004,8 @@ The `orPattern` method also accepts a quantifier as its **second argument** (aft
0 commit comments