The 'commit and don't look back' group
An atomic group (?>...) matches like a normal group, BUT once it succeeds, the engine refuses to backtrack into it. Whatever it consumed is locked in. The engine moves forward; if the rest of the pattern fails, it doesn't try alternatives inside the atomic group.
Why this matters
Backtracking inside loops is the source of all catastrophic regex performance. (\w+)*X against a long string of word characters with no X at the end will try every possible split of the input among the iterations of \w+ — exponential.
Make the inner group atomic: (?>\w+)*X. Now once \w+ grabs everything it can, the engine doesn't try to give characters back. Fails immediately, linear time.
Engine support
- Python
re: Atomic groups added in Python 3.11. Earlier versions: install third-partyregex. - PCRE, Perl, Java, Ruby, .NET: Supported.
- JavaScript: Not supported (yet).
- RE2/Go: Doesn't backtrack at all, so doesn't need atomic groups.
Mental model
Atomic groups are how you tell the engine "once you've matched this, don't second-guess yourself." Use them whenever a pattern's inner section won't have a valid alternative match if the first one fails — which is almost always for things like "one or more digits," "one or more word characters," "the rest of this line."