AXO AXO - 4 years ago 87
C# Question

RightToLeft flag and atomic group

Why

a(?>.*)
does not match
abc
when RightToLeft is activated?

The way I expected it to work is:


  1. The right-most character (
    c
    ) does not match the
    a
    .

  2. The next character is
    b
    . It again won't match
    a
    .

  3. a
    matches
    a
    ; the greedy
    .
    inside the atomic group will match b and c. There are no more characters and the regex is satisfied.


Answer Source

The problem isn't really related to the RightToLeft option. The string is searched in a reverse order, but other thing that needs to be taken in account is that the pattern tokens are tested in the reverse order too. So (?>.*) first, and then a.

An atomic group forbids backtracking. Since .* is by default greedy, it takes c, then b, then a (in other words it takes "abc"), but because of the atomic group .* will not give a character back (the a) and the pattern will fail.

(Note that without the RightToLeft option you are in the same situation in which you try to find occurrences in "cba" with the pattern (?>.*)a)

A more simple example without the RightToLeft option, always with the string "abc" but with the pattern a(?>.*)c:

a         =>   a
a(?>.*)   =>   abc
a(?>.*)c  =>   FAIL since (?>.*) don't give back the c

If this time I use a non-capturing group instead of an atomic group: a(?:.*)c

a         =>   a
a(?:.*)   =>   abc
a(?:.*)c  =>   FAIL
a(?:.*)   =>   ab (Backtracking)
a(?:.*)c  =>   abc (Succeed)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download