Documentation for version: 0.41.2
In certain situations an amount of occurrences is desired, but not the occurrences themselves.
In that case, it's recommended to consider
Pattern.count(), instead of performing full matches
Pattern.count() accepts a
string subject, and returns the number of occurrences of
the pattern in the subject.
Pattern.count() is called with a subject that doesn't match the pattern,
Every use of
Pattern::of() with an invalid pattern causes
Number of occurrences in
Search.count() can also be used to
retrieve the number of occurrences of the pattern in the subject.
Matcher implements PHP interface
\Countable, and so can be used with PHP methods,
#Performance predicating a subject
It might appear suitable to use
count() to ascertain whether a given subject matches the
count() doesn't return any matches.
While that is true, that using
Pattern.count() is more performant than
it still iterates all of the occurrences of the pattern in the subject.
The recommended approach for such predication is with
count() goes through each occurrence of a pattern in the subject counting it,
test() returns right after matching the first occurrence.