I'm aware of many of the great answer about atomic grouping, e.g., Confusion with Atomic Grouping - how it differs from the Grouping in regular expression of Ruby?
My question is simple: so alternation in atomic grouping is useless, right?
Some examples:
a(?>bc|b)c
will never matchabc
, actually it will never tryb
part in the()
(?>.*|b*)[ac]
will never match any string since.*
matches them all and is discarded.
Do I understand it right?
Some test code in perl
just in case it might be helpful
sub tester {
my ($txt, $pat) = @_;
if ($txt =~ $pat) {
print "${pat} matches ${txt}\n";
} else {
print "No match\n";
}
}
$txt = "abcc";
$pat = qr/a(?>bc|b)c/;
tester($txt, $pat);
$txt = "bbabbbabbbbc";
$pat = qr/(?>.*)c/;
tester($txt, $pat);
$pat = qr/(?>.*|b*)[ac]/;
tester($txt, $pat);
$txt = "abadcc";
$pat = qr/a(?>b|dc)c/;
tester($txt, $pat);
I found an explanation in here that kinda answers my question.
It (atomic grouping) tells the RegEx engine that once it has found a matching subpattern not to backtrack on any of the quantifiers or alternative that may be inside of it.