(?>pattern)(PCRE/Java; not native in JS or Python re)
Example
PCRE:(?>\d+)\s matches '123 ' but NOT by backtracking into digits
Python(regex module):import regex
regex.search(r'(?>\d+):','123:')
Output
Matches '123:' -- once the digits are consumed, the engine cannot give them back
Note Atomic groups discard all backtracking positions once the group matches. They are a performance optimization to prevent catastrophic backtracking. In Python, use the third-party 'regex' module. In JS, there is no direct equivalent -- simulate with a possessive quantifier mindset or restructure the pattern.
JS:// DANGER: This can freeze your browser/server!// /(a+)+$/.test('aaaaaaaaaaaaaaaaaaaaaaaaaab')// The engine tries exponential combinations before failingPy:# Same risk: re.search(r'(a+)+$', 'aaa...ab')
Output
Extremely slow or hangs -- exponential backtracking
Note Catastrophic backtracking occurs when the regex engine explores an exponential number of paths. It happens with nested quantifiers applied to overlapping patterns. Prevention: avoid nested quantifiers on similar character sets, use atomic groups or possessive quantifiers, add anchors, or restructure the pattern. This is one of the most common causes of regex-based DoS (ReDoS) vulnerabilities.
Frequently asked questions
How does Regular Expressions handle performance regex?
Regular Expressions covers this with 2 copy-ready snippets on this page. The "Atomic Groups (Concept)" snippet in Regular Expressions uses `(?>pattern) (PCRE/Java; not native in JS or Python re)`.
Which pattern does the Regular Expressions example use?
The "Atomic Groups (Concept)" snippet uses `(?>pattern) (PCRE/Java; not native in JS or Python re)`, from the Advanced Techniques section of the Regular Expressions cheat sheet.
What other Regular Expressions snippets are shown for "performance regex"?
Besides "Atomic Groups (Concept)", this page also shows "Catastrophic Backtracking".
Is there anything to watch out for?
Yes. For "Atomic Groups (Concept)": Atomic groups discard all backtracking positions once the group matches. They are a performance optimization to prevent catastrophic backtracking. In Python, use the third-party 'regex' module. In JS, there is no direct equivalent -- simulate with a possessive quantifier mindset or restructure the pattern.