R

TestRegex

← Back to Blog

What is Atomic Grouping (?>...)?

Atomic grouping is a power-user feature supported in PCRE, Java, and modern Ruby. It's essentially a "Lock the Door" instruction.

The Concept

Normally, if a match fails later in the string, the regex engine backtracks into previous groups to try different permutations. An atomic group (?>...) tells the engine: "Once you leave this group, throw away the key. Don't backtrack into here."

(?>a+)b

Against string aaaa: The atomic group eats all 'a's. It then checks for 'b'. Fails. Since it's atomic, it refuses to give back an 'a' to see if that helps. The match fails immediately, saving cycles.