Boundary constructs

Boundary constructs allow us to specify where our matches should start or stop by restricting our matches to certain boundaries. We avoid matching unwanted text by adding restrictions to the start and stop positions of the matching text using boundary matchers. Here are all the boundary constructs available in Java regular expressions:

Boundary Matcher Name Meaning
\b Word boundary; position between a word and a non-word character
\B Non-word boundary; it compliments \b and asserts true wherever \b asserts false
^ Line-start anchor, which matches the start of a line
$ Line-end anchor, which matches just before the optional line break at the end of a line
\A Permanent start of input; in a multiline input using ...

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.