Skip to content

Commit 2b5b783

Browse files
committed
Added some examples to README
1 parent 8fe8136 commit 2b5b783

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,49 @@ $regexFacade = new RegexExecutor();
5555
* mixed __replaceCallback__(string $pattern, callable $callback, mixed $subject, integer $limit = -1)
5656
* ReplaceResult __replaceCallbackAndCount__(string $pattern, callable $callback, mixed $subject, integer $limit = -1)
5757

58+
For each method, the pattern can be a string, a Flux object, or a PhpVerbalExpression object.
59+
60+
Examples:
61+
---------
62+
63+
#### getAllMatches:
64+
65+
```php
66+
try {
67+
$result = $regexFacade->getAllMatches('/\d+/', 'ab12cd34ef56');
68+
echo 'Number of matches: ' . $result->getMatchesCount() . PHP_EOL; // Prints 3
69+
echo 'First match: ' . $result->getMatchAt(0)->getFullMatch() . PHP_EOL; // Prints 12
70+
71+
// Iterate over results
72+
foreach ($result as $index => $match) {
73+
echo "Match at index {$index} is " . $match->getFullMatch() . PHP_EOL;
74+
}
75+
} catch (\Exception $e) {
76+
echo 'An error occurred executing getAllMatches';
77+
}
78+
```
79+
80+
#### getOneMatch:
81+
82+
```php
83+
try {
84+
$result = $regexFacade->getOneMatches('/\d+/', 'ab12cd34ef56');
85+
if (!empty($result)) {
86+
echo 'Match: ' . $result->getFullMatch() . PHP_EOL; // Prints 12
87+
}
88+
} catch (\Exception $e) {
89+
echo 'An error occurred executing getOneMatch';
90+
}
91+
```
92+
93+
#### replaceAndCount:
94+
95+
```php
96+
try {
97+
$result = $this->executor->replaceAndCount('/\d+/', 'potato', 'ab12cd34ef56');
98+
echo 'Number of replacements: ' . $result->getReplacements() . PHP_EOL;
99+
echo 'Replaced string: ' . $result->getResult() . PHP_EOL;
100+
} catch (\Exception $e) {
101+
echo 'An error occurred executing replaceAndCount';
102+
}
103+
```

0 commit comments

Comments
 (0)