Hi, @sylvester-bullitt, @alan-kilborn, @peterjones and All,
Some observations about in-line modifiers :
Let’s consider the text :
1 abc
2 abC
3 aBc
4 aBC
5 Abc
6 AbC
7 ABc
8 ABC
The regex (?-i)a((?i)b)c matches lines 1 and 3 and the group 1 contains the letter b or B
Using a non-capturing group, the full syntax of the regex becomes (?-i)a(?:(?i)b)c and matches the same lines 1 and 3 but, this time, no group is defined
As a convenient shorthand, in case of a non-capturing group, the option may appear between the question mark and the colon, giving the alternate syntax (?-i)a(?i:b)c, which produces the same results as the former regex
Note that, because options are not reset until the end of a subpattern, if that subpattern contains alternatives, an option setting in one branch does affect all the subsequent branches !
So, assuming the text :
1 ab
2 aB
3 Ab
4 AB
5 cd
6 cD
7 Cd
8 CD
The regex
((?-i)ab|CD), with
group1 and the regex
(?-i:ab|CD), with a
non-capturing group, match,
both, lines
1 and
8,
only
REMARK :
Note that the previous rule is always valid if, when choosing between the different alternatives, the regex engine is not aware of a later option. For instance, let’s consider the regex (?-i)(a(?i)b|cd)
In this regex, due to the initial (?-i) modifier, the search should be done in a sensitive way, except for the part (?i)b, which should match letter b or B. However, this regex matches the lines 1 and 2 and lines from 5 to 8, in the above text !
For instance, when the regex engine is located, right before the first letter of line 8, and choose the second alternative, in order to process c, rather than a, it does not know about the caseless option, applied to letter b. Still, it does match the uppercase letter C because the (?i) modifier is carried on into the cd alternative, as well !
Best Regards,
guy038