However, don't forget to use the required group around alternation

Often, we see regex patterns that use alternation, and around the alternation, we use anchors or boundary matchers without safeguarding the alternation expression in a group. Note that the ^, $, \A, \Z, \z anchors and the \b boundary matcher have a higher precedence than the alternation character, | (pipe)

So, consider a regular expression written as follows:

^com|org|net$ 

It will also match computer, organization, and internet, though the intent probably was to match only com, net, and org. This is because the start anchor, ^, is being applied to com only and the end anchor, $, is being applied to net, whereas org is not anchored at all.

This regular expression should be ...

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.