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

Examples using boundary constructs

Which regex should be used to match "at" when the input is 'Hat at work"?

    \bat\b

The preceding regex should be used because \b (word boundary) stops the regex engine to match at in Hat, because \bat\b can match full words only.

What should be regex if we only want to match at in Hat but not the one that was matched in the preceding regex?

    \Bat\b

Now, this regex will match at that is a part of Hat because \B asserts a position that is between two word characters or a position between two non-word characters. Because of the presence of \B in the regex, it matches at only in Hat but not the word at.

If the input is suppress expression press depression, what will be the matches if the regex is \Bpress\B ...

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