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

Get Java 9 Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.