Documentation for version: 0.41.2
PatternList allows you to perform performance-optimized operations on a collection of
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
To create a pattern list, simply pass an
array of patterns to
Pattern::list() method. The elements
array can either be
string or instance of
Pattern, or a mixture of those.
PatternList with prepared patterns#
Any instance of
Pattern can be added into the list, including patterns created with
Additionally, for convenience, passing
string is allowed into
Pattern::list() behaves in exactly the same way, as wrapping the
Using either type is acceptable in a single
To be precise,
(Pattern|string) as argument.
After you have created
PatternList, you can match the list against a certain subject.
true when at least one of the patterns matches the subject, and
when none of the patterns matches the subject.
true when at least one of the patterns fails to match the subject, and
when all the patterns match the subject.
true only when all of the patterns match the subject, and
when any of the patterns fails to match the subject.
true only when none of the patterns match the subject, and
when any of the patterns matches the subject.
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
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
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
PatternList.replace() also supports Perl-Compatible group references in the replacements,
available with method
with() doesn't accept references, so
with('[$1]') will replace occurrences with
"[$1]" exactly, while
withReferences('[$1]') will replace occurrences with the
$1 will be replaced with the capturing group of index
The references syntax is identical to
Pattern.replace().withReferences(), so all syntaxes of
references are supported: