Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between atomic and non-capturing groups?

Tags:

regex

  1. What is an atomic group, ((?>expr)) and what is it used for? In https://www.regular-expressions.info/atomic.html, the only example is when expr is alternation, such as the regex a(?>bc|b)c matches abcc but not abc. Are there examples with expr not being alternation?
  2. Are atomic and non-capturing groups, ((?:expr)) the same thing?
like image 782
Tim Avatar asked Jun 27 '11 05:06

Tim


1 Answers

Atomic groups (and the possessive modifier) are useful to avoid catastrophic backtracking - which can be exploited by malicious users to trigger denial of service attacks by gobbling up a server's memory.

Non-capturing groups are just that -- non-capturing. The regex engine can backtrack into a non-capturing group; not into an atomic group.

like image 75
Denis de Bernardy Avatar answered Sep 18 '22 15:09

Denis de Bernardy