Issue with Java Regex \b

后端 未结 2 1568
一整个雨季
一整个雨季 2020-12-03 12:29

I tried \\b (This means the last character of a word) in the Java Regexp, but this doesn\'t work.

String input = \"aaa aaa\";
Pattern pattern =          


        
相关标签:
2条回答
  • 2020-12-03 12:43

    In Java, "\b" is a back-space character (char 0x08), which when used in a regex will match a back-space literal.

    You want the regex a\b, which in java is coded by escaping the back-slash, like this:

    "a\\b"
    

    btw, you are only partially correct about the meaning of regex \b - it actually means "word boundary" (either the start or end of a word).

    0 讨论(0)
  • 2020-12-03 12:48

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

    0 讨论(0)
提交回复
热议问题