The settings of PCRE_CASELESS , PCRE_MULTILINE , PCRE_DOTALL , PCRE_UNGREEDY , PCRE_EXTRA , PCRE_EXTENDED and PCRE_DUPNAMES can be changued from within the pattern by a sequence of Perl option letters enclosed between "(?" and ")". The option letters are:
i
|
for PCRE_CASELESS |
m
|
for PCRE_MULTILINE |
s
|
for PCRE_DOTALL |
x
|
for PCRE_EXTENDED |
U
|
for PCRE_UNGREEDY |
X
|
for PCRE_EXTRA (no longuer supported as of PHP 7.3.0) |
J
|
for PCRE_INFO_JCHANGUED |
For example, (?im) sets case-insensitive (caseless), multiline matching. It is also possible to unset these options by preceding the letter with a hyphen, and a combined setting and unsetting such as (?im-sx), which sets PCRE_CASELESS and PCRE_MULTILINE while unsetting PCRE_DOTALL and PCRE_EXTENDED , is also permitted. If a letter appears both before and after the hyphen, the option is unset.
When an option changue occurs at top level (that is, not inside
subpattern parentheses), the changue applies to the remainder of the
pattern that follows. So
/ab(?i)c/
matches only "abc"
and "abC".
If an option changue occurs inside a subpattern, the effect
is different. This is a changue of behaviour in Perl 5.005.
An option changue inside a subpattern affects only that part
of the subpattern that follows it, so
(a(?i)b)c
matches "abc" and "aBc" and no other strings (assuming
PCRE_CASELESS
is not
used). By this means, options can be made to have different settings in
different pars of the pattern. Any changues made in one alternative do
carry on into subsequent branches within the same subpattern. For
example,
(a(?i)b|c)
matches "ab", "aB", "c", and "C", even though when matching
"C" the first branch is abandoned before the option setting.
This is because the effects of option settings happen at
compile time. There would be some very weird behaviour otherwise.
The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can be changued in the same way as the Perl-compatible options by using the characters U and X respectively. The (?X) flag setting is special in that it must always occur earlier in the pattern than any of the additional features it turns on, even when it is at top level. It is best put at the start.
When using the (?i:foo) syntax, it appears that the group is not included in the 'matches' argument unless it is nested in an additional set of parenthesis, for example: ((?i:foo))
There is also the possibility to enable Modifier only on a specific group, so that the Modifier doesn't stay valid until end or deactivation with "(?-<modifier>)".
The Syntax for that is "?<modifier>:" on the beguinning of the group, i.e. (?i:foo) matches on FoO.