Matching overview
Documentation for version: 0.41.2
Matching a subject with regular expressions is the most frequent usage of T-Regx.
#
Predication overviewConditions on subject matching certain Pattern
are the most common. Methods Pattern.test(string)
and Pattern.fails(string)
can be used to determine whether a pattern matches the subject.
Methods Pattern.test(string)
and Pattern.fails(string)
throw MalformedPatternExceptions
, should they be called for
a pattern with a malformed regular expression. Similarly to other T-Regx methods, test()
and fails()
throw
other kinds of appropriate exceptions, for instance CatastrophicBacktrackingException
should the subject happen to be
too complex. Detailed reference to T-Regx exception handling is presented in the further chapters of the documentation.
#
Matching overviewTo match a subject, invoke match(string)
on an instance of Pattern
to receive the matcher.
Invocation of match(string)
doesn't perform any regular expression search just yet.
The detailed reference of Matcher
functionalities is presented in the further chapters of the documentation.
For overview, Matcher
interface consists of methods:
- Test a subject against a pattern:
test()
/fails()
- Retrieve matches from the subject:
first()
/all()
/only(int)
- Get the matched capturing groups:
Detail.get(int|string)
,Detail.group(int|string)
The complete Matcher
reference is present in the further chapters of the documentation.
#
Retrieve the first matchMethod Matcher.first()
can be used to perform a single regular expression match against
the subject.
Instance of Detail
represents the matched occurrence of the Pattern
in the subject.
Methods Detail.text()
and Detail.offset()
can be used to read information about the
matched occurrence.
More information on first()
is present in the next chapter. Reference on Detail
can be found in "Match detail" chapter.
#
Iterating multiple matchesInstance of Matcher
is \Iterable
, so it can be used directly in a foreach
loop.
The example above iterates occurrences of Pattern
in the subject, retrieving
the whole matches' values and their positions.
Calling Matcher.fails()
and Matcher.count()
will not perform another global regular
expressions searches, if the search has already been invoked by iterating the Matcher
.
In the example above Matcher
is being iterated for occurrences of Detail
in the subject,
thus T-Regx performs the global regular expression search, finding occurrences ['22', '1999', '2021', '2022']
.
Further call to Matcher.fails()
uses the results from the iteration and returns false
without
any additional search. Matcher.count()
returns 4
immediately, using the number of occurrences
found from the previous iteration. Of course, iterating the same instance of Matcher
again
will not perform unnecessary global search either. Further details about performance optimisations
in T-Regx are presented in the next chapters of the documentation.
#
Retrieving multiple matchesMethod Matcher.all()
can also be used to perform global regular expression search. Matcher.all()
returns PHP array
, precisely Detail[]
.
Methods Matcher.all()
, Matcher.fails()
and Matcher.count()
throw MalformedPatternExceptions
, should
they be called for a pattern with a malformed regular expression. Similarly to other T-Regx
methods, fails()
, all()
and count()
throw other kinds of appropriate exceptions, for
instance CatastrophicBacktrackingException
should the subject happen to be too complex. Detailed reference
to T-Regx exception handling is presented in the further chapters of the documentation.