pattern/functions
is a set of helper functions, simplifying the usage of Pattern
of standard T-Regx library.
See documentation at t-regx.com.
- Installation
- Overview
- Documentation
- Examples
pattern_test()
pattern_fails()
pattern_count()
pattern_match_first()
pattern_match_optional()
pattern_match_ref()
pattern_match_all()
pattern_replace()
pattern_replace_callback()
pattern_split()
pattern_cut()
pattern_filter()
pattern_reject()
pattern_search()
- Examples
- API reference
- Frequently asked questions
- Comparison against
preg_
functions - Sponsors
- License
Installation for PHP 7.1-8.2 and later:
Package pattern/functions
is an addon to rawr/t-regx
.
composer require pattern/functions
Installing pattern/functions
will also install core rawr/t-regx
.
Currently, T-Regx composer package is named rawr/t-regx
. In the future, with release 1.0 the T-Regx package will be
renamed to pattern/pattern
.
T-Regx library is an object-oriented approach to regular expressions, with
classes Pattern
, Matcher
etc. Find more in "Introduction to T-Regx".
On the other hand, a simplified approach is possible with helper functions in pattern/helpers
package. It adds
simplified functions for an easier start with the library.
Examples with helper functions:
<?php
if (pattern_test('\w+', $_GET['subject'])) {
$matches = pattern_match_all('\w+', $_GET['subject']);
foreach ($matcher as $match) {
echo "Found a match $match at position {$match->offset()}!";
}
echo "Occurrences found: " . pattern_count('\w+', $_GET['subject']);
}
else {
echo "No matches found :/";
}
return pattern_split('\w+', $_GET['subject']);
<?php
return pattern_replace('\w+', $_GET['subject'], '***');
<?php
return pattern_replace_callback('\w+', $_GET['subject'], fn (Detail $match) => '***');
<?php
try {
$match = pattern_match_first('\w+', $_GET['subject'], fn (Detail $match) => '***');
}
catch (MalformedPatternException $exception) {
// used improper regular expression
}
catch (CatastrophicBacktrackingException $exception) {
// catastrophic backtracking while matching
}
Helper functions utilize the same implementation as Pattern
in the core library.
Available functions:
- Predication:
pattern_test()
- returnstrue
, if pattern matches the subjectpattern_count()
- returns number of occurrences of pattern in the subject
- Retrieve match/matches:
pattern_match_first()
- returns the first matched occurrence, asDetail
pattern_match_optional()
- returns an optional matched occurrence, asDetail|null
pattern_match_ref()
- populates&Detail
via ref-argument and returnstrue
/false
pattern_match_all()
- returns all matched occurrences, asDetail[]
- Replacing:
pattern_replace()
- replaces occurrences of pattern in the subject with given stringpattern_replace_callback()
- replaces occurrences of pattern in the subject via given callbackpattern_prune()
- removes occurrences of pattern from the subject
- Splitting:
pattern_split()
- returnsstring[]
separated by occurrences of the pattern in the subjectpattern_cut()
- returns a two-element array separated by a single occurrence of pattern in the subject
- Filtering array
pattern_filter()
- acceptsstring[]
and returnsstring[]
with only subjects matching the patternpattern_reject()
- acceptsstring[]
and returnsstring[]
with subjects that don't match the pattern
Scroll to "API Reference".
Scroll to "Exception Reference".
Scroll to "Frequently asked questions".
-
Check whether the pattern matches the subject:
if (pattern_test('[A-Z][a-z]+', $_GET['username']) === true) { }
Function
pattern_test()
is another notation of$pattern = Pattern::of('[A-Z][a-z]+'); if ($pattern->test($_GET['username'])) { }
Examples of error handling
try { $matched = pattern_test($pattern, $subject); } catch (MalformedPatternException $exception) { } catch (CatastrophicBacktrackingException $exception) { } catch (SubjectEncodingException $exception) { } catch (RecursionException $exception) { }
T-Regx library is based on exceptions, and so are helper functions. Function
pattern_test()
doesn't issue any warnings, notices and doesn't set any C-like status codes. -
Retrieve the first occurrence of pattern in the subject:
/** * @var Detail $match */ $match = pattern_match_first('\w+', 'Welcome to the jungle'); $match->text(); $match->offset(); $match->group(2); $match->toInt();
Function
pattern_match_first()
is another notation of$pattern = Pattern::of('[A-Z][a-z]+'); $matcher = $pattern->match($_GET['username']); /** * @var Detail $match */ $match = $matcher->first();
-
Retrieve the matched occurrence of pattern in a subject, or
null
for an unmatched subject:/** * @var ?Detail $match */ $match = pattern_match_optional('\w+', 'Welcome to the jungle'); if ($match === null) { // pattern not matched } else { $match->text(); $match->offset(); $match->group(2); $match->toInt(); }
More about
Detail
can be found in "Match details". -
Read matched occurrence details of pattern in a subject, and populate
Detail
as a reference argument:if (pattern_match_ref('\w+', 'Welcome to the jungle', $match)) { /** * @var Detail $match */ $match->text(); $match->offset(); $match->group(2); $match->toInt(); } else { // pattern not matched }
More about
Detail
can be found in "Match details". -
Retrieve all matched occurrences of pattern in the subject:
/** * @var Detail[] $matches */ $matches = pattern_match_all('\w+', 'Winter is coming'); foreach ($matches as $match) { }
Function
pattern_match_all()
is another notation of$pattern = Pattern::of('\w+'); $matcher = $pattern->match('Winter is coming'); $matches = $matcher->all(); foreach ($matches as $match) { }
-
Replace occurrences of pattern in the subject with a given
string
replacement orcallable
:$slug = pattern_replace('\s+', 'We do not sow', '-');
$slug = pattern_replace_callback('\s+', 'We do not sow', fn (Detail $match) => '-');
Functions
pattern_replace()
andpattern_replace_callback()
are another notation of$pattern = Pattern::of('\s+'); $replace = $pattern->replace('We do not sow'); $slug = $replace->with('-'); $slug = $replace->callback(fn (Detail $match) => '-');
More about Detail
can be found
in "Match details".
Predication
pattern_test()
returnstrue
if$pattern
matches the$subject
,false
otherwise.pattern_test(string $pattern, string $subject, string $modifiers=''): bool;
pattern_fails()
returnsfalse
if$pattern
matches the$subject
,true
otherwise.pattern_fails(string $pattern, string $subject, string $modifiers=''): bool;
pattern_count()
returns the number of occurrences of$pattern
in$subject
.pattern_count(string $pattern, string $subject, string $modifiers=''): int;
Matching
pattern_match_first()
returns the first occurrence of pattern in the subject asDetail
; throwsSubjectNotMatchedException
if the pattern doesn't match the subject.pattern_match_first(string $pattern, string $subject, string $modifiers=''): Detail;
pattern_match_optional()
returns the first occurrence of pattern in the subject asDetail
; returnsnull
if the pattern doesn't match the subject.pattern_match_optional(string $pattern, string $subject, string $modifiers=''): Detail|null;
pattern_match_ref()
returnstrue
and populates&Detail
as ref-argument with the first occurrence of pattern in the subject; returnsfalse
if the pattern doesn't match the subject.pattern_match_ref(string $pattern, string $subject, ?Detail &$refDetail, string $modifiers=''): bool;
pattern_match_all()
returns all occurrences of pattern in the subject asDetail[]
; returns an empty array if the pattern doesn't match the subject.pattern_match_all(string $pattern, string $subject, string $modifiers=''): Detail[];
pattern_search()
returns all occurrences of pattern in the subject asstring[]
; returns an empty array if the pattern doesn't match the subject.pattern_search(string $pattern, string $subject, string $modifiers=''): array;
Replacing
pattern_replace()
replaces all occurrences of pattern in a subject with a given replacement.pattern_replace(string $pattern, string $subject, string $replacement, string $modifiers=''): string;
pattern_replace_callback()
replaces all occurrences of pattern in a subject via the specified callback. The callback is passedDetail
as the only argument, and only acceptsstring
as return type.pattern_replace_callback(string $pattern, string $subject, callable $callback, string $modifiers=''): string;
pattern_prune()
removes all occurrences of pattern from a subject.pattern_prune(string $pattern, string $subject, string $modifiers=''): string;
Splitting
pattern_split()
separates the subject by occurrences of pattern in the subject. If two occurrences of pattern are found in the subject next to each other, then an empty string is present in the returned array to indicate it. A part or whole separator can be included in the returned array by adding a capturing group in the pattern.pattern_split(string $pattern, string $subject, string $modifiers=''): string[];
pattern_cut()
splits a subject into exactly two elements, which are returned asstring[]
. The separating pattern occurrence is not included in the result,pattern_cut()
only returns a two-element array. If there are more occurrences of the pattern in the subject, or subject is not matched at all, thenUnevenCutException
is thrown.pattern_cut(string $pattern, string $subject, string $modifiers=''): string[];
Filtering subject lists
pattern_filter()
acceptsstring[]
of subjects, and returnsstring[]
which only contains subjects which match the pattern.pattern_filter(string $pattern, string[] $subjects, string $modifiers=''): string[];
pattern_reject()
acceptsstring[]
of subjects, and returnsstring[]
which only contains subjects which do not match the pattern.pattern_reject(string $pattern, string[] $subjects, string $modifiers=''): string[];
Helper for Pattern
pattern()
is an alias forPattern::of()
.pattern(string $pattern, string $modifiers=''): Pattern;
All of the functions in pattern/functions
library throw the specified exceptions:
MalformedPatternException
- when pattern is given not conforming to the regular expression syntaxRecursionException
- when recursion limit was exhausted while matching the subjectCatastrophicBacktrackingException
- when backtracking limit was exhausted while matching the subjectSubjectEncodingException
- when improperly encoded subject is used in unicode-modeJitStackLimitException
- when JIT-compilation optimisation could not be added, due to exhausted limit
Functions in pattern/functions
are thin wrappers around functionalities in core library.
pattern()
- identical toPattern::of()
pattern_test()
- identical toPattern.test()
pattern_fails()
- identical toPattern.fails()
pattern_match_first()
- identical toMatcher.first()
pattern_match_all()
- identical toMatcher.all()
pattern_search()
- identical toPattern.search()
pattern_replace()
- identical toReplace.with()
pattern_replace_callback()
- identical toReplace.callback()
pattern_prune()
- identical toPattern.prune()
pattern_count()
- identical toPattern.count()
pattern_split()
- identical toPattern.split()
pattern_cut()
- identical toPattern.cut()
pattern_filter()
- identical toPattern.filter()
pattern_reject()
- identical toPattern.reject()
Functions which aren't directly present in core library, but can be easily simulated.
pattern_match_optional()
- checksMatcher.test()
, then returns eitherMatcher.first()
ornull
pattern_match_ref()
- checksMatcher.test()
, then passes&$ref
withMatcher.first()
ornull
-
Why is
pattern/functions
a separate library, and not a part of core library-
Most object-oriented projects develop their applications only using classes, which are not "polluted" by rouge functions in the global namespace. To add global functions to the project is not in our competence.
However, some users actually prefer the simplified approach, and they should be able to easily start their project. Because of that
pattern/functions
is available.
-
-
Why functions
pattern_replace()
andpattern_replace_callback()
are separate functions, instead of a single function with dynamic type checkstring
andcallable
?- We decided to separate the functions, because certain PHP
string
are alsocallable
(e.g.'strlen'
,'strtoupper'
, etc.).
- We decided to separate the functions, because certain PHP
-
Why isn't there
pattern_quote()
?- Using
preg_quote()
is a more procedural approach to building regular expressions. In T-Regx, the recommended approach is using Prepared patterns:Pattern::inject()
andPattern::template()
. Additionally,preg_quote()
doesn't quote all of the necessary characters, for example in comments and also whitespaces in/x
mode.
- Using
-
Why I shouldn't use
@
withpattern/functions
?- Notation
@
is supposed to suppress PHP warnings/errors/notices, but T-Regx library doesn't issue warnings, errors and notices, so@
is redundant. Both core library andpattern/functions
only throw exceptions, sotry
/catch
should be used.
- Notation
-
Why isn't there
pattern_last_error()
?- Such function is not necessary, since all functions in
pattern/functions
as well as in core library throw suitable exceptions on error.
- Such function is not necessary, since all functions in
-
Should I choose core T-Regx library or
pattern/functions
? -
Can I do with
pattern/functions
everything I can do with core library?- Actually, core library is much more powerful than just
pattern/functions
. Most notably core library offers Prepared patterns andPattern::list()
, among other functionalities.
- Actually, core library is much more powerful than just
-
How does performance
pattern/functions
relate to core library?- In case of a single call (for example comparing
pattern_test()
andPattern.test()
) there is no difference in performance. - In case of repeated calls with the same pattern, then the core library approach is technically more performant, because of reused compiled pattern; however the difference can only be shown with millions of repeated matches.
- In case of a single call (for example comparing
-
How does T-Regx library prevent fatal errors?
- Certain input values cause PHP and PCRE to fatally end and terminate the PHP process (for example, returning a
non-stringable object from
preg_replace_callback()
terminates the application). T-Regx handles that by utilizing a carefulif
-ology, and when potentially dangerous value is returned, an exception is thrown instead.
- Certain input values cause PHP and PCRE to fatally end and terminate the PHP process (for example, returning a
non-stringable object from
pattern_
functions accept an undelimited regular expression, whilepreg_
functions argument must be delimited.pattern_test()
returnstrue
/false
, whilepreg_match()
returns0
/1
.pattern_
functions handle errors with exceptions, whilepreg_
functions use warnings, notices, errors, fatal errors and error codes withpreg_last_error()
.pattern_
functions exposes details asDetail
interface, whilepreg_
returns nested arrays.pattern_match_all()
returns an array ofDetail
objects, whilepreg_match_all()
populates(string|int)[][]
via&$ref
.
- Andreas Leathley - developing SquirrelPHP
- BarxizePL - Thanks!
phpunit-data-provider
T-Regx is MIT licensed.