Advantages of non-capturing groups

A non-capturing group lets us use the grouping inside a regular expression without changing the numbers assigned to the back references (explained in the next section). This can be very useful in building large and complex regular expressions.

Non-capturing groups also give us the flexibility to add or remove groups from a long regular expression with multiple groups. If we have to insert a new group or remove an existing group, then the numerical order changes for every group on the right-hand side of the new or removed group.

Using a non-capturing group instead of a capturing group saves memory, as the regular expression engine doesn't need to store groups in buffers, thus optimizing the overall regex ...

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.