简体   繁体   English

Java Regex问题\\ b

[英]Issue with Java Regex \b

I tried \\b (This means the last character of a word) in the Java Regexp, but this doesn't work. 我在Java Regexp中尝试了\\b (这意味着单词的最后一个字符),但这不起作用。

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 in the problem? 有什么问题?

In Java, "\\b" is a back-space character (char 0x08 ), which when used in a regex will match a back-space literal. 在Java中, "\\b"是一个后退字符(字符0x08 ),当在正则表达式中使用时将与后退字符匹配。

You want the regex a\\b , which in java is coded by escaping the back-slash, like this: 你想要正则表达式a\\b ,在java中通过转义反斜杠来编码,如下所示:

"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). 顺便说一句,你只是部分正确的正则表达式的含义\\b - 它实际上意味着“单词边界”(一个单词的开头或结尾)。

Java字符串中的文字反斜杠需要转义,因此正则表达式\\b变为"\\\\b"作为Java字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM