O'Reilly logo

Java 9 Regular Expressions by Anubhava Srivastava

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required