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
Overview#
Using 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, asstringtoInt()- value of the group, cast tointisInt()- whether the group is a valid integer (e.g.truefor 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, ornullof 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 text#
To get a value of a capturing group from, use Detail.get():
Group details#
You can chain Detail.group() with a variety of methods, you can use to get
more details about the group.
Index, name and identifier#
Groups 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, ornullif the group is not named
Optional Groups#
Method 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 arguments#
matched()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,-1or any type other thanstringorint).
Nonexistent Groups#
Method 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 arguments#
groupExists()will throw\InvalidArgumentException, when used with an invalid group (i.e.2group,-1or any type other thanstringorint).
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 names#
When any group method is called with an invalid group name, for example:
then \InvalidArgumentException is thrown.
Composite groups#
Detail.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 names#
Method 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 count#
Method groupsCount() returns the number of capturing groups (without duplicates of named and regular groups)
Other occurrences#
Method 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.
Complication with J modifier#
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-Depth#
note
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.