Java Regex \ b的问题 [英] Issue with Java Regex \b
本文介绍了Java Regex \ b的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Java Regexp中尝试过 \b
(这意味着单词的最后一个字符),但这不起作用。
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 = Pattern.compile("(a\b)");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println("Found this wiki word: " + matcher.group());
}
有什么问题?
推荐答案
在Java中,\ b
是一个后退字符(char 0x08
),在正则表达式中使用时将与后退文字匹配。
In Java, "\b"
is a back-space character (char 0x08
), which when used in a regex will match a back-space literal.
你想要正则表达式 a \\ n \\ b
,在java中通过转义反斜杠进行编码,如下所示:
You want the regex a\b
, which in java is coded by escaping the back-slash, like this:
"a\\b"
btw,你只是部分正确的正则表达式的含义 \ 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).
这篇关于Java Regex \ b的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文