Multiple patterns
Documentation for version: 0.41.2
PatternList
allows you to perform performance-optimized operations on a collection of
patterns.
While using an array of Pattern
in a loop is perfectly viable, it doesn't leave any room
for performance optimisation. Operations of PatternList
are designed to use minimal resources,
such as a single call to PCRE. On the other hand - looping over Pattern
will issue as many
calls as there are patterns in the array.
#
Available methods- Constructing
PatternList
- Available methods
#
Constructing the pattern listTo create a pattern list, simply pass an array
of patterns to Pattern::list()
method. The elements
in the array
can either be string
or instance of Pattern
, or a mixture of those.
PatternList
with Pattern
instances#
PatternList
with prepared patterns#
Any instance of Pattern
can be added into the list, including patterns created with Pattern::inject()
,
Pattern::template()
, Pattern::mask()
and Pattern::builder()
.
PatternList
with string
patterns#
Additionally, for convenience, passing string
is allowed into Pattern::list()
.
Passing string
into Pattern::list()
behaves in exactly the same way, as wrapping the
string in Pattern::of()
beforehand.
Using either type is acceptable in a single Pattern::list()
:
To be precise, Pattern::list()
accepts (Pattern|string)[]
as argument.
#
Available methods#
Matching either patternAfter you have created PatternList
, you can match the list against a certain subject.
Method PatternList.testAny()
returns true
when at least one of the patterns matches the subject, and false
when none of the patterns matches the subject.
Method PatternList.failAny()
returns true
when at least one of the patterns fails to match the subject, and false
when all the patterns match the subject.
#
Matching collective patternsMethod PatternList.testAll()
returns true
only when all of the patterns match the subject, and false
when any of the patterns fails to match the subject.
Method PatternList.failAll()
returns true
only when none of the patterns match the subject, and false
when any of the patterns matches the subject.
#
Subject modification#
Prune a subject with the listMethod PatternList.prune()
allows to remove all occurrences of all the patterns in the list from the subject.
prune()
is useful for cleaning subject of unwanted elements.
It's preferable over iterating the patterns and calling prune()
individually, because of
performance optimisations in PatternList.prune()
.
Overlapping patterns are being removed sequentially, based on the order of patterns in the list. In the example above, first the mails will be removed, then the leading spaces and the the trailing spaces.
Here's an example to illustrate the order of prune()
:
with()
#
Collective replace Method PatternList.replace()
works very similarly to Pattern.replace()
. To replace
a subject with the collective list of patterns, call method replace()
, which accepts the
subject as an argument.
In this example, we'll replace every HTML tag and every number with string "XXX"
:
Please, refer to the StackOverflow question regarding matching HTML entities with regular expressions.
PatternList.replace()
also supports Perl-Compatible group references in the replacements,
available with method withReferences()
:
Remember that with()
doesn't accept references, so with('[$1]')
will replace occurrences with
literal string "[$1]"
exactly, while withReferences('[$1]')
will replace occurrences with the
replacement string "[$1]"
where $1
will be replaced with the capturing group of index 1
.
The references syntax is identical to Pattern.replace().withReferences()
, so all syntaxes of
references are supported: $1
, \1
and ${1}
.