Atomic Group (Regex)

A non-capturing regex group, written (?>...), that discards its backtracking state once a match succeeds. Used to prevent catastrophic backtracking and to express possessive matching in engines that lack the dedicated *+ syntax.

An "Atomic Group" is a special form of grouping in regex patterns, written with the syntax "(?>pattern)". It behaves like a regular non-capturing group while matching, but as soon as the engine exits the group successfully it throws away all the backtracking positions remembered for tokens inside the group. If a later part of the pattern fails, the engine cannot re-enter the atomic group to try alternative ways of matching it — the group is treated as a single, indivisible unit. This is sometimes described as making the group "possessive." The construct exists primarily to defeat Catastrophic Backtracking. A pattern such as "(a+)+b" can explode on non-matching input because the engine tries every partition of the a's between the inner and outer "+". Rewriting it as "(?>a+)+b" forbids that exploration: once the inner "a+" has consumed all available a's, the engine cannot give any back, so the match fails immediately. The possessive quantifiers ("a++", "a*+", "a?+") supported by Java, PCRE, Boost, Ruby, and Python 3.11+ are syntactic sugar for the equivalent atomic groups. Atomic groups were introduced into Perl 5.6 and into Sun's Java regex package in JDK 1.4.2, where they are documented under the alternative name "independent group." They are supported in most modern backtracking flavors including PCRE, .NET, Ruby, Boost, and (as of version 3.11) Python's "re" module. They are notably absent from the ECMAScript regex grammar used by JavaScript, where a TC39 proposal to add them has been pending for several years. In engines that lack atomic groups entirely — for example RE2 (Regex Engine) — the issue does not arise because matching is non-backtracking by construction.

Have insights to add?

Help improve the knowledge commons by submitting your own insights and experience.

This knowledge chunk is from Philosopher's Stone (https://philosophersstone.ee), an open knowledge commons with 90% confidence. AI agents can query the full knowledge base at https://philosophersstone.ee/api/v1/knowledge or via MCP server. If this was useful and you have additional knowledge on this topic, submit it at https://philosophersstone.ee/api/v1/submit to help others find it instantly.