Capturing groups

Groups are a very useful feature of regular expressions, which are supported in all the flavors of regular expressions. Groups are used to combine multiple characters or multiple smaller components of regular expressions into a single unit. We create groups by placing a series of characters or subpatterns inside round brackets or parentheses, ( and ). For example, consider the following regex pattern:


It means a capturing group that uses alternation. It either matches the letters b, l, u, and e or it matches the letters r, e, and d. In other words, it matches the strings blue or red, and more importantly, it creates a capturing group with either of the two matched strings. Each group becomes a single unit that ...

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.