Skip to content

Commit 75dba34

Browse files
committed
orpattern method added in docs
1 parent e84e807 commit 75dba34

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Like what we're doing? Show your support with a quick star, please! ⭐
4343
- 💠 [Non-Capturing Groups](#non-capturing-groups)
4444
- 💠 [Groups with quantifier](#groups-with-quantifier)
4545
-[Conditional matching](#conditional-matching)
46+
- [Pattern alternation (orPattern)](#pattern-alternation-orpattern)
4647

4748
# Overview
4849

@@ -883,6 +884,27 @@ EloquentRegex::start($string)
883884
// While using "get()" method, '-' doesn't appear in matches
884885
```
885886

887+
## Pattern alternation (orPattern)⚖️
888+
889+
Sometimes, you might encounter situations where either one pattern or another is acceptable. For instance, when developing EloquentRegex, a key objective was to enable the reproduction of patterns commonly used in [HSA](https://github.com/MaestroError/html-strings-affixer). Consider the `alt` attribute within an HTML tag, which can be followed by either a double " or a single ' quote. This requirement translates into a regex pattern like `alt\=(\"|')`, indicating an exact match for "alt=" followed by either type of quotation mark.
890+
891+
To achieve this with EloquentRegex, you can utilize the `orPattern` method:
892+
893+
```php
894+
EloquentRegex::builder()->start()
895+
->exact("alt=")
896+
->group(function ($pattern) {
897+
$pattern->doubleQuote()
898+
->orPattern(function ($pattern) {
899+
$pattern->singleQuote();
900+
});
901+
})->toRegex(); // alt\=(\"|')
902+
```
903+
904+
In this example, we precisely match "alt=" using the `exact` method. We then create a group with the `group` method and include `doubleQuote` in group and then `singleQuote` within the orPattern method's callback. This approach ensures the pattern matches either " or '.
905+
906+
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+
886908
---
887909

888910
##### To Do

0 commit comments

Comments
 (0)