Use atomic group to avoid backtracking and fail fast

Recall from Chapter 6, Exploring Zero-Width Assertions, Lookarounds, and Atomic Groups, that an atomic group is a non-capturing group that exits the group and throws away all the alternative positions remembered by any token inside the group, after the first match of the pattern inside the group. Thus, it avoids backtracking to attempt all the alternatives present in the group.

Due to this very characteristic of atomic groups, the use of atomic groups in certain scenarios saves many unnecessary backtracking steps and speeds up the overall regex execution.

So, use this atomic group:

\btra(?>ck|ce|ining|de|in|nsit|ns|uma)\b 

It is better to use the preceding atomic group instead of the following ...

Get Java 9 Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.