Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE (PHP <7.3)

/
\b(?>foobar|foot|foo)\b
/
g

Description

Atomic Groups are non-capturing and once a match is made will exit the atomic group and throw away all backtracks. Use Atomic Groups for optimising performance.

A non-atomic expression \b(foobar|foot|foo)\b and a test string of foots will:

match foo of foobar => fail and backtrack to the 2nd alternative match foot of foot => fail as \b is exprected and backtrack to the 3rd alternative match foo of foo => and fail to match. An atomic group expression \b(?>foobar|foot|foo)\b and a test string of foots will:

match foot of foot => fail as expects /b but has s and exits group and releases all backtracking alternatives Note: An atomic group \b(?>foobar|foot|foo|foots)\b will not match a test string of foots as it will test using the 2nd alternative and fail, releasing backtrackings.

A non-atomic group \b(foobar|foot|foo)\b will match a test string of foots as it tests each alternative.

Submitted by anonymous - 9 years ago