Skip to content

Commit a9c9bd8

Browse files
committed
Quantifiers section done
1 parent 5fb8398 commit a9c9bd8

File tree

1 file changed

+105
-36
lines changed

1 file changed

+105
-36
lines changed

README.md

Lines changed: 105 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -207,58 +207,131 @@ if ($result) {
207207

208208
```
209209

210+
_Note: You can use `EloquentRegex::builder()->pattern()` if you need just build a regex without source string_
211+
210212
Custom pattern builder supports a wide range of character classes and all special chars. Also, `literal` or `exact` method could be used to match exact string you need, or `char` method could be used to match exact character. The full list of pattern builder methods is comming soon. Before that, you can check this files out:
211213

212214
- [Character Classes](https://github.com/MaestroError/eloquent-regex/blob/documentation-and-examples/src/Traits/BuilderPatternTraits/CharacterClassesTrait.php)
213215
- [Special characters](https://github.com/MaestroError/eloquent-regex/blob/documentation-and-examples/src/Traits/BuilderPatternTraits/SpecificCharsTrait.php)
214216
- [Groups](https://github.com/MaestroError/eloquent-regex/blob/documentation-and-examples/src/Traits/BuilderPatternTraits/GroupsTrait.php)
215217
- [Anchors](https://github.com/MaestroError/eloquent-regex/blob/documentation-and-examples/src/Traits/BuilderPatternTraits/AnchorsTrait.php)
216218

217-
#### Quantifiers
219+
## Applying Quantifiers
220+
221+
Quantifiers in regular expressions are symbols or sets of symbols that specify how many instances of a character, group, or character class must be present in the input for a match to be found. EloquentRegex enhances the way quantifiers are used, making it simpler and more intuitive to define the frequency of pattern occurrences.
222+
223+
### Optional Elements
224+
225+
To make an element optional, use '?'. This matches zero or one occurrence of the preceding element.
226+
227+
```php
228+
// Matches a string that may or may not contain a dash
229+
$result = EloquentRegex::start($yourString)->exact("123")->dash('?')->exact("456")->check();
230+
// Result would be true in both cases of $yourString: "123456" & "123-456"
231+
```
232+
233+
### Specifying a Range
234+
235+
For specifying a range of occurrences, use a string with two numbers separated by a comma '2,5'. This matches the preceding element at least and at most the specified times.
236+
237+
```php
238+
// Matches a string with 2 to 5 spaces
239+
$result = EloquentRegex::start($yourString)->text()->space('2,5')->digits()->check();
240+
// Result: the "someText 234" would return true, the "someText 234" false
241+
```
242+
243+
### One or More
244+
245+
To match one or more occurrences of an element, use '+', '1+', '1>' or 'oneOrMore'. This ensures the element appears at least once.
246+
247+
```php
248+
// Matches strings with one or more backslashes
249+
$result = EloquentRegex::start("\\\\")->backslash('1+')->check();
250+
// Result: true (if one or more backslashes are found)
251+
```
252+
253+
### Zero or More
254+
255+
The '\*' quantifier matches zero or more occurrences of the preceding element.
256+
257+
```php
258+
// Matches strings with zero or more forward slashes
259+
$result = EloquentRegex::start($yourString)->alphanumeric()->dot('0+')->check();
260+
// Result would be true in both cases of $yourString: "test258..." & "test"
261+
```
262+
263+
### Exact Number
218264

219-
Available values for quantifiers as argument:
265+
To match an exact number of occurrences, directly specify the number.
220266

221-
- zeroOrMore = `"zeroOrMore"`, `"0>"`, `"0+"`
222-
- oneOrMore = `"oneOrMore"`, `"1>"`, `"1+"`
223-
- optional = `"optional"`, `"?"`, `"|"`
267+
```php
268+
// Matches strings with exactly 2 underscores
269+
$result = EloquentRegex::start($yourString)->digits()->underscore('2')->digits()->check();
270+
// Result would be true in cases $yourString: "1235__158", but "1235___158" and "1235_158" will be false
271+
272+
```
273+
274+
### Custom Character Sets and groups
275+
276+
You can apply quantifiers to custom character sets and groups as second argument after the callback, matching a specific number of occurrences.
277+
278+
```php
279+
// Matches strings with exactly 3 periods or colons
280+
$regex = EloquentRegex::builder()->start()
281+
->charSet(function ($pattern) {
282+
$pattern->period()->colon();
283+
}, '3')->toRegex();
284+
// Result: ([\.\:]){3}
285+
```
286+
287+
### Quantifier values
288+
289+
In [Special characters](https://github.com/MaestroError/eloquent-regex/blob/documentation-and-examples/src/Traits/BuilderPatternTraits/SpecificCharsTrait.php)
290+
and
291+
[Groups](https://github.com/MaestroError/eloquent-regex/blob/documentation-and-examples/src/Traits/BuilderPatternTraits/GroupsTrait.php)Available
292+
nearly all methods allowing quantifiers with values:
293+
294+
- Zero or More = `"zeroOrMore"`, `"0>"`, `"0+"`, `"*"`
295+
- One or More = `"oneOrMore"`, `"1>"`, `"1+"`, `"+"`
296+
- Optional (Zero or One) = `"optional"`, `"?"`, `"|"`
297+
- exact amount = `2`, `"5"`
298+
- range = `"{0,5}"`
299+
300+
Example: `->exact("hello world", false, "1+")`
224301

225-
Examples: `->exact("hello world", false, "1+")`
302+
But
303+
[Character Classes](https://github.com/MaestroError/eloquent-regex/blob/documentation-and-examples/src/Traits/BuilderPatternTraits/CharacterClassesTrait.php)
304+
have different approach, lets take `digits` as example:
305+
306+
```php
307+
// By defualt it is set as One or More
308+
EloquentRegex::start($yourString)->digits();
309+
310+
// You can completly remove quantifier by passing 0 as first argument
311+
EloquentRegex::start($yourString)->digits(0);
312+
313+
// You can specify exact amount of digit by passing int
314+
EloquentRegex::start($yourString)->digits(5);
315+
316+
// You can specify range of digits by adding "Range" to the method
317+
EloquentRegex::start($yourString)->digitsRange(1, 5); // Matches from 1 to 5 digits
318+
```
226319

227320
##### To Do
228321

229-
- Add needed options for new patterns:
322+
- Add options for new patterns:
230323
- usernameLength: Set minimum and maximum length for the username part of the email.
231324
- dateFormat, timeFormat: Specify the format of date and time (e.g., MM-DD-YYYY, HH:MM).
232325
- Consider to register Patterns like options using key (name) => value (class) pairs (check performance) ✔️ (_No significant change before 50+ patterns_)
233326

234-
- Extend BuilderPattern, try to add methods:
235-
236-
- group(callable $callback): Creates a grouped subpattern.✔️
237-
- nonCapturingGroup(callable $callback): Creates a non-capturing group.✔️
238-
- orPattern(): Alternation, allowing for multiple possibilities.✔️
239-
- lookAhead(callable $callback): Positive lookahead assertion.✔️
240-
- lookBehind(callable $callback): Positive lookbehind assertion.✔️
241-
- negativeLookAhead(callable $callback): Negative lookahead assertion.✔️
242-
- negativeLookBehind(callable $callback): Negative lookbehind assertion.✔️
243-
- Raw regex methods for advanced users.✔️
244-
- BuilderPattern should be able to reproduce patterns used in HSA✔️
245-
246-
- Add benchmarks and tests for search against large data ✔️
247-
- Add Feature Tests for BuilderPattern ✔️
248-
- Remove need for "end" method in BuilderPattern ✔️
249-
- Add Dockblocs and comments for new methods ✔️
250-
251-
- Add facade for Laravel ✔️
252-
- Wrap Builder in class for static start ✔️
253-
- "string" and "source" for builder start ✔️
254-
- "start" and "pattern" for builderPattern start ✔️
255327
- Write documentation (add credit for https://regexr.com/ and ChatGPT)
256-
- Add automated tests on PR creation or on marging to main branch ✔️
257-
258-
- Make Tests for quantifiers (add grouping) ✔️
259-
- Make quantifiers available for special chars ✔️
260328
- Return collection on get method if laravel is available.
261329
- Create quick start guide and add in Docs.
330+
- Add advanced usage section in Docs:
331+
- Options and Assertions: Detailed explanation of options, how to apply them, and their effects on patterns.
332+
- Filters in Extraction: Using options as filters during extraction and the types of filters available.
333+
- Regex Flags: Guide on applying regex flags to patterns for specialized matching behavior.
334+
- Grouping and Capturing: How to use groups (capturing and non-capturing) and apply quantifiers to them.
262335

263336
##### Coming next
264337

@@ -273,7 +346,3 @@ Examples: `->exact("hello world", false, "1+")`
273346
```
274347
275348
```
276-
277-
```
278-
279-
```

0 commit comments

Comments
 (0)