Skip to content

Commit 5fb8398

Browse files
committed
Custom pattern section
1 parent 1662daf commit 5fb8398

File tree

3 files changed

+204
-146
lines changed

3 files changed

+204
-146
lines changed

README.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,14 @@ textOrNumbers(int $minLength, int $maxLength, int $minUppercase, int $minLowerca
111111
email(int $maxLength, array|string $onlyDomains, array|string $onlyExtensions)`
112112
```
113113

114-
````php
114+
```php
115115
url(array|string $onlyProtocol)`
116+
```
117+
116118
```php
117119
// $onlyDomains & $onlyExtensions array or string separated by comma "org,com"
118120
domainName(int $maxLength, array|string $onlyDomains, array|string $onlyExtensions)`
119-
````
121+
```
120122

121123
```php
122124
date()
@@ -153,7 +155,9 @@ password(int $minLength, int $minUppercase, int $minNumbers, int $minSpecialChar
153155
```
154156

155157
```php
156-
// $restrictTags & $onlyTags array or string separated by comma `"script, style"`. It isn't recomended to use both option in simultaneously
158+
// $restrictTags & $onlyTags array or string
159+
// separated by comma `"script, style"`.
160+
// It isn't recomended to use both option simultaneously
157161
htmlTag(array|string $restrictTags, array|string $onlyTags)
158162
```
159163

@@ -171,6 +175,45 @@ filePath(int $isDirectory, bool $isFile, bool $fileExists,string $pathType) -
171175
filePathWin(int $isDirectory, bool $isFile, bool $fileExists)
172176
```
173177

178+
Didn't it cover all your needs? Let's take a look to the custom patterns section.
179+
180+
## Custom Patterns
181+
182+
For scenarios where predefined patterns do not suffice, EloquentRegex allows you to define custom patterns using the start or customPattern methods as initiator:
183+
184+
```php
185+
EloquentRegex::start($yourString);
186+
// Or
187+
EloquentRegex::customPattern($yourString);
188+
```
189+
190+
_Note: They does the exaclty same thing, you can use your favorite one_
191+
192+
### Creating a Custom Pattern
193+
194+
You can start building a custom pattern to match a specific string format, such as a custom ID format that starts with letters followed by digits:
195+
196+
```php
197+
$result = EloquentRegex::start('ID123456')
198+
->literal('ID')
199+
->digitsRange(1, 10)
200+
->check();
201+
202+
if ($result) {
203+
echo "The string matches the custom ID format!";
204+
} else {
205+
echo "The string does not match the custom ID format.";
206+
}
207+
208+
```
209+
210+
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:
211+
212+
- [Character Classes](https://github.com/MaestroError/eloquent-regex/blob/documentation-and-examples/src/Traits/BuilderPatternTraits/CharacterClassesTrait.php)
213+
- [Special characters](https://github.com/MaestroError/eloquent-regex/blob/documentation-and-examples/src/Traits/BuilderPatternTraits/SpecificCharsTrait.php)
214+
- [Groups](https://github.com/MaestroError/eloquent-regex/blob/documentation-and-examples/src/Traits/BuilderPatternTraits/GroupsTrait.php)
215+
- [Anchors](https://github.com/MaestroError/eloquent-regex/blob/documentation-and-examples/src/Traits/BuilderPatternTraits/AnchorsTrait.php)
216+
174217
#### Quantifiers
175218

176219
Available values for quantifiers as argument:

src/Patterns/BuilderPattern.php

Lines changed: 2 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
use Maestroerror\EloquentRegex\Traits\BuilderPatternTraits\CharacterClassesTrait;
88
use Maestroerror\EloquentRegex\Traits\BuilderPatternTraits\SpecificCharsTrait;
99
use Maestroerror\EloquentRegex\Traits\BuilderPatternTraits\AnchorsTrait;
10+
use Maestroerror\EloquentRegex\Traits\BuilderPatternTraits\GroupsTrait;
1011
use Maestroerror\EloquentRegex\Builder;
1112

1213

1314
class BuilderPattern extends BasePattern {
1415

1516
// BuilderPattern doesn't need the "execute" method (src\Traits\Pattern.php)
16-
use CharacterClassesTrait, SpecificCharsTrait, AnchorsTrait;
17+
use CharacterClassesTrait, SpecificCharsTrait, AnchorsTrait, GroupsTrait;
1718

1819
/**
1920
* @var array Array of options for the pattern.
@@ -174,147 +175,5 @@ public function lazy(): self {
174175
return $this;
175176
}
176177

177-
/**
178-
* Adds a new set of characters.
179-
*
180-
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
181-
* @return self
182-
*/
183-
public function charSet(callable $callback, ?string $q = null): self {
184-
$subPattern = new self();
185-
$callback($subPattern);
186-
$p = '[' . $subPattern->getPattern() . ']';
187-
$this->pattern .= $q ? $this->applyQuantifier($p, $q) : $p;
188-
return $this;
189-
}
190-
191-
/**
192-
* Adds a new set of denied characters.
193-
*
194-
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
195-
* @return self
196-
*/
197-
public function negativeCharSet(callable $callback, ?string $q = null): self {
198-
$subPattern = new self();
199-
$callback($subPattern);
200-
$p = '[^' . $subPattern->getPattern() . ']';
201-
$this->pattern .= $q ? $this->applyQuantifier($p, $q) : $p;
202-
return $this;
203-
}
204-
205-
/**
206-
* Adds a new grouped subpattern.
207-
*
208-
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
209-
* @return self
210-
*/
211-
public function group(callable $callback, ?string $q = null): self {
212-
$subPattern = new self();
213-
$callback($subPattern);
214-
$p = $subPattern->getPattern();
215-
$this->pattern .= $q ? $this->applyQuantifier($p, $q) : '(' . $p . ')';
216-
return $this;
217-
}
218-
219-
/**
220-
* Adds a new non-capturing grouped subpattern.
221-
*
222-
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
223-
* @return self
224-
*/
225-
public function nonCapturingGroup(callable $callback, ?string $q = null): self {
226-
$subPattern = new self();
227-
$callback($subPattern);
228-
$p = '(?:' . $subPattern->getPattern() . ')';
229-
$this->pattern .= $q ? $this->applyQuantifier($p, $q) : $p;
230-
return $this;
231-
}
232178

233-
/**
234-
* Adds an alternation pattern.
235-
*
236-
* @param callable $callback A callback that receives a BuilderPattern instance to define the alternation.
237-
* @return self
238-
*/
239-
public function orPattern(callable $callback, ?string $q = null): self {
240-
$builder = new self();
241-
$callback($builder);
242-
$p = $builder->getPattern();
243-
$this->pattern .= $q ? '|' . $this->applyQuantifier($p, $q) : '|' . $p;
244-
return $this;
245-
}
246-
247-
/**
248-
* Adds a positive lookahead assertion.
249-
*
250-
* @param callable $callback A callback that receives a BuilderPattern instance to define the assertion.
251-
* @return self
252-
*/
253-
public function lookAhead(callable $callback): self {
254-
$builder = new self();
255-
$callback($builder);
256-
$this->pattern .= '(?=' . $builder->getPattern() . ')';
257-
return $this;
258-
}
259-
260-
/**
261-
* Adds a positive lookbehind assertion.
262-
*
263-
* @param callable $callback A callback that receives a BuilderPattern instance to define the assertion.
264-
* @return self
265-
*/
266-
public function lookBehind(callable $callback): self {
267-
$builder = new self();
268-
$callback($builder);
269-
$this->pattern .= '(?<=' . $builder->getPattern() . ')';
270-
return $this;
271-
}
272-
273-
/**
274-
* Adds a negative lookahead assertion.
275-
*
276-
* @param callable $callback A callback that receives a BuilderPattern instance to define the assertion.
277-
* @return self
278-
*/
279-
public function negativeLookAhead(callable $callback): self {
280-
$builder = new self();
281-
$callback($builder);
282-
$this->pattern .= '(?!' . $builder->pattern . ')';
283-
return $this;
284-
}
285-
286-
/**
287-
* Adds a negative lookbehind assertion.
288-
*
289-
* @param callable $callback A callback that receives a BuilderPattern instance to define the assertion.
290-
* @return self
291-
*/
292-
public function negativeLookBehind(callable $callback): self {
293-
$builder = new self();
294-
$callback($builder);
295-
$this->pattern .= '(?<!' . $builder->pattern . ')';
296-
return $this;
297-
}
298-
299-
/**
300-
* Adds a raw regex string to the pattern.
301-
*
302-
* @param string $regex The raw regex string to add.
303-
* @return self
304-
*/
305-
public function addRawRegex(string $regex): self {
306-
$this->pattern .= $regex;
307-
return $this;
308-
}
309-
310-
/**
311-
* Wraps a given regex string in a non-capturing group and adds it to the pattern.
312-
*
313-
* @param string $regex The regex string to wrap and add.
314-
* @return self
315-
*/
316-
public function addRawNonCapturingGroup(string $regex): self {
317-
$this->pattern .= '(?:' . $regex . ')';
318-
return $this;
319-
}
320179
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
namespace Maestroerror\EloquentRegex\Traits\BuilderPatternTraits;
4+
5+
trait GroupsTrait {
6+
7+
/**
8+
* Adds a new set of characters.
9+
*
10+
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
11+
* @param ?string $q a Quantifier
12+
* @return self
13+
*/
14+
public function charSet(callable $callback, ?string $q = null): self {
15+
$subPattern = new self();
16+
$callback($subPattern);
17+
$p = '[' . $subPattern->getPattern() . ']';
18+
$this->pattern .= $q ? $this->applyQuantifier($p, $q) : $p;
19+
return $this;
20+
}
21+
22+
/**
23+
* Adds a new set of denied characters.
24+
*
25+
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
26+
* @param ?string $q a Quantifier
27+
* @return self
28+
*/
29+
public function negativeCharSet(callable $callback, ?string $q = null): self {
30+
$subPattern = new self();
31+
$callback($subPattern);
32+
$p = '[^' . $subPattern->getPattern() . ']';
33+
$this->pattern .= $q ? $this->applyQuantifier($p, $q) : $p;
34+
return $this;
35+
}
36+
37+
/**
38+
* Adds a new grouped subpattern.
39+
*
40+
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
41+
* @param ?string $q a Quantifier
42+
* @return self
43+
*/
44+
public function group(callable $callback, ?string $q = null): self {
45+
$subPattern = new self();
46+
$callback($subPattern);
47+
$p = $subPattern->getPattern();
48+
$this->pattern .= $q ? $this->applyQuantifier($p, $q) : '(' . $p . ')';
49+
return $this;
50+
}
51+
52+
/**
53+
* Adds a new non-capturing grouped subpattern.
54+
*
55+
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
56+
* @param ?string $q a Quantifier
57+
* @return self
58+
*/
59+
public function nonCapturingGroup(callable $callback, ?string $q = null): self {
60+
$subPattern = new self();
61+
$callback($subPattern);
62+
$p = '(?:' . $subPattern->getPattern() . ')';
63+
$this->pattern .= $q ? $this->applyQuantifier($p, $q) : $p;
64+
return $this;
65+
}
66+
67+
/**
68+
* Adds an alternation pattern.
69+
*
70+
* @param callable $callback A callback that receives a BuilderPattern instance to define the alternation.
71+
* @param ?string $q a Quantifier
72+
* @return self
73+
*/
74+
public function orPattern(callable $callback, ?string $q = null): self {
75+
$builder = new self();
76+
$callback($builder);
77+
$p = $builder->getPattern();
78+
$this->pattern .= $q ? '|' . $this->applyQuantifier($p, $q) : '|' . $p;
79+
return $this;
80+
}
81+
82+
/**
83+
* Adds a positive lookahead assertion.
84+
*
85+
* @param callable $callback A callback that receives a BuilderPattern instance to define the assertion.
86+
* @return self
87+
*/
88+
public function lookAhead(callable $callback): self {
89+
$builder = new self();
90+
$callback($builder);
91+
$this->pattern .= '(?=' . $builder->getPattern() . ')';
92+
return $this;
93+
}
94+
95+
/**
96+
* Adds a positive lookbehind assertion.
97+
*
98+
* @param callable $callback A callback that receives a BuilderPattern instance to define the assertion.
99+
* @return self
100+
*/
101+
public function lookBehind(callable $callback): self {
102+
$builder = new self();
103+
$callback($builder);
104+
$this->pattern .= '(?<=' . $builder->getPattern() . ')';
105+
return $this;
106+
}
107+
108+
/**
109+
* Adds a negative lookahead assertion.
110+
*
111+
* @param callable $callback A callback that receives a BuilderPattern instance to define the assertion.
112+
* @return self
113+
*/
114+
public function negativeLookAhead(callable $callback): self {
115+
$builder = new self();
116+
$callback($builder);
117+
$this->pattern .= '(?!' . $builder->pattern . ')';
118+
return $this;
119+
}
120+
121+
/**
122+
* Adds a negative lookbehind assertion.
123+
*
124+
* @param callable $callback A callback that receives a BuilderPattern instance to define the assertion.
125+
* @return self
126+
*/
127+
public function negativeLookBehind(callable $callback): self {
128+
$builder = new self();
129+
$callback($builder);
130+
$this->pattern .= '(?<!' . $builder->pattern . ')';
131+
return $this;
132+
}
133+
134+
/**
135+
* Adds a raw regex string to the pattern.
136+
*
137+
* @param string $regex The raw regex string to add.
138+
* @return self
139+
*/
140+
public function addRawRegex(string $regex): self {
141+
$this->pattern .= $regex;
142+
return $this;
143+
}
144+
145+
/**
146+
* Wraps a given regex string in a non-capturing group and adds it to the pattern.
147+
*
148+
* @param string $regex The regex string to wrap and add.
149+
* @return self
150+
*/
151+
public function addRawNonCapturingGroup(string $regex): self {
152+
$this->pattern .= '(?:' . $regex . ')';
153+
return $this;
154+
}
155+
156+
}

0 commit comments

Comments
 (0)