|
SubpatternsSubpatterns are delimited by parentheses (round brackets), which can be nested. Marking part of a pattern as a subpattern does two things:
For example, if the string "the red king" is matched against
the pattern
The fact that plain parentheses fulfill two functions is not
always helpful. There are often times when a grouping subpattern
is required without a capturing requirement. If an
opening parenthesis is followed by "?:", the subpattern does
not do any capturing, and is not counted when computing the
number of any subsequent capturing subpatterns. For example,
if the string "the white queen" is matched against the
pattern
As a convenient shorthand, if any option settings are required at the start of a non-capturing subpattern, the option letters may appear between the "?" and the ":". Thus the two patterns (?i:saturday|sunday) (?:(?i)saturday|sunday) match exactly the same set of strings. Because alternative branches are tried from left to right, and options are not reset until the end of the subpattern is reached, an option setting in one branch does affect subsequent branches, so the above patterns match "SUNDAY" as well as "Saturday".
It is possible to name a subpattern using the syntax
Sometimes it is necessary to have multiple matching, but alternating
subgroups in a regular expression. Normally, each of these would be given
their own backreference number even though only one of them would ever
possibly match. To overcome this, the (?:(Sat)ur|(Sun))day
Here (?|(Sat)ur|(Sun))day
Using this pattern, both |