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:

tra(?>ck|ce|ining|de|in|nsit|ns|uma) 

It is better to use the preceding atomic group instead of the following non-capturing group:

tra(?:ck|ce|ining|de|in|nsit|ns|uma) 

The difference in behavior will be evident when matching an input string, such as tracker, which fails to match.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.248.162