Atomic groups
An atomic group is a non-capturing group that throws away all the alternative positions remembered by any token inside the group when the matching process exits the group after the first match of the pattern inside the group. Thus, it avoids backtracking to attempt all the alternatives present in the group.
Here is the syntax:
(?>regex)
Here, the regex may contain alternative patterns. On the other hand, a non-atomic group will allow backtracking; it will try to find the first match and then if the matching ahead fails, it will backtrack and try to find the next match in alternation, until a match for the entire expression is found or all the possibilities are exhausted.
To understand it better, let's take an example of a regular expression using a non-atomic group:
^foo(d|die|lish)$
The input string here is foodie
.
It will match the starting pattern foo
and then the first alternative d
. It fails at this time because the end anchor, $
, requires that we must be at the end of the...