Question

I am aware that \bword\B behaves like a word\B using regex in Java. (in terms of matching engine)

Why would it be possible? would not it more reasonable if it was en exception (syntax exception)? moreover it is also used by oracle in this tutorial as an explanatory example (\bdog\B). If you do not know the answer, please feel more than free to not respond. Thanks in advance.

Was it helpful?

Solution

\B is the negated version of \b. \B matches at every position where \b does not. Effectively, \B matches at any position between two word characters as well as at any position between two non-word characters.

\b allows you to perform a "whole words only" search using a regular expression in the form of \bword\b. A "word character" is a character that can be used to form words. All characters that are not "word characters" are "non-word characters". It is more exact to say \b matches before and after an alphanumeric sequence


Above is from here.

In your example of \bdog\B, it will match "doggie" because you asked it to match words and partial words.

\bdog\b will fail on "doggie" because "dog" is the whole word whereas "dog" in "doggie" is a partial word.

It's conditional depending on what it's touching.

Moreover, dog\B will match "dog" in the phrase "I have a doggie"

dog\b will match "dog" in the phrase "I have adog gie"

dog\B will not match "dog" in the phrase "I have adog gie"

\bdog\b will not match "dog" in the phrase "I have adog gie"

but \Bdog\b will match "dog" in the phrase "I have adog gie"

This SO answer provides a detailed explanation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top