Problem with Java Regex \ b

I tried \b (this means the last character of the word) in Java Regexp, but this does not work.

 String input = "aaa aaa"; Pattern pattern = Pattern.compile("(a\b)"); Matcher matcher = pattern.matcher(input); while (matcher.find()) { System.out.println("Found this wiki word: " + matcher.group()); } 

What is the problem?

+4
source share
2 answers

In Java, "\b" is a backspace character (char 0x08 ), which when used in a regular expression will match a backspace literal.

You need the regular expression a\b , which is encoded in java by escaping bask-slash, for example:

 "a\\b" 

btw, you only partially correctly relate the value of regex \b - this actually means "word boundary" (either the beginning or the end of the word).

+15
source

The literal backslashes in Java strings need escaping, so regex \b becomes "\\b" as a Java string.

+3
source

Source: https://habr.com/ru/post/1389898/


All Articles