Capturing groups
When using pattern()->match()
and pattern()->replace->callback()
, some methods receive a callback that accepts Detail
details object. These methods are: forEach()
, map()
, flatMap()
, filter()
, callback()
.
The details can be used to get concise information about the matched occurrence, such as its value
(i.e. "the whole match"
), capturing groups and their UTF-8/raw offsets, limits, indexes, other matches as well as the
used subject (although it could also be pass as a closure parameter) and more.
note
#
OverviewUsing Detail
, you gain access complete information about capturing groups:
get(int|string)
- capturing group text valuegroup(int|string)
- capturing group details, with:text()
- value of the group, asstring
toInt()
- value of the group, cast toint
isInt()
- whether the group is a valid integer (e.g.true
for group(\d+)
)- offsets of matched values in the subject:
- character offsets (UTF-8 safe) -
offset()
- byte offsets -
byteOffset()
- character offsets (UTF-8 safe) -
index()
- ordinal value of the capturing group in a patternname()
- name of the capturing group, ornull
of group is not namedall()
- other matched occurrences of the groupmatched(int|string)
- whether the group was matched by the subjectgroupExists(int|string)
- whether group was used in a patterngroups()
/namedGroups()
- interface for composite operations for all groupsgroupNames()
- string list of named groups used in a patterngroupsCount()
- counts number of capturing groups (without duplicates of named and regular groups)
note
All types of group syntax: (?<name>)
, (?'name')
and (?P<name>)
are considered "named". Regular, indexed groups
are use with syntax ()
. Group (?:)
is considered a non-capturing group.
#
Group textTo get a value of a capturing group from, use Detail.get()
:
#
Group detailsYou can chain Detail.group()
with a variety of methods, you can use to get
more details about the group.
#
Index, name and identifierGroups can be referred to either by an index or by name, if the group in a pattern is named. What was the group referred with is called an identifier. If group was referred to by an index, then the index is the identifier.
T-Regx has 2 separate methods for each of the group reference method:
index()
- returns the ordinal number of a groupname()
- returns the name of a group, ornull
if the group is not named
#
Optional GroupsMethod matched(int|string)
allows you to verify whether a given group was matched by the subject:
It'll work just as well with named groups:
Although method $detail->matched(int|string)
is the preferred way - same effect can be achieved with
using $detail->group(int|string)->matched()
:
#
Invalid groups and argumentsmatched()
will throwNonexistentGroupException
, when used with a non-existent group (i.e.asdf
).matched()
will throw\InvalidArgumentException
, when used with an invalid group (i.e.2group
,-1
or any type other thanstring
orint
).
#
Nonexistent GroupsMethod groupExists(int|string)
allows you to verify whether the group was used in a pattern:
It'll work just the same with regular (not named) groups:
#
Invalid groups and argumentsgroupExists()
will throw\InvalidArgumentException
, when used with an invalid group (i.e.2group
,-1
or any type other thanstring
orint
).
note
Usages of groupExists()
are rather infrequent, because rarely patterns are dynamic - they're constant much more often;
hence the developer doesn't have to check whether the group exists.
#
Invalid group namesWhen any group method is called with an invalid group name, for example:
then \InvalidArgumentException
is thrown.
#
Composite groupsDetail.groups()
and Detail.namedGroups()
return a list of capturing groups, same
as Detail.group(string|int)
.
If a group is not matched, it will be represented as null
in the list.
#
Group namesMethod groupNames()
returns a simple string[]
with names of the capturing groups in order:
If a group isn't named, it's represented by null
:
#
Groups countMethod groupsCount()
returns the number of capturing groups (without duplicates of named and regular groups)
#
Other occurrencesMethod group()->all()
allows you to gain access to the occurrence of the group in other matches in the subject.
If the group is not matched in other occurrences, its value in all()
result array will be null
:
In other words Detail.group($x).all()
is a collection of occurrences of group $x
in all other matches.
J
modifier#
Complication with Complication with J
modifier is a rather advanced matter, and not necessary for everyday use.
If you don't seek "in-depth" understanding of capturing groups, feel free to skip this chapter.
To learn more, go to Capturing groups - J modifier.
#
Groups In-Depthnote
Groups In-Depth is a rather advanced matter, and not necessary for everyday use. If you don't seek "in-depth" understanding of capturing groups, feel free to skip this chapter.
To learn more, go to Capturing groups - in depth.