[英]Java Regex Word Boundaries
Hi I have the following code which is meant to find the word "is" but not when it is within another string so the word "this" should not return a match so I use \\b. 嗨,我有以下代码,意思是找到单词“是”,但不是当它在另一个字符串中,所以单词“this”不应返回匹配,所以我使用\\ b。 But the following code does not find a match and I cant figure out why? 但是下面的代码没有找到匹配,我无法弄清楚为什么?
public static void main(String[] args) {
String a = "This island is beautiful.";
Pattern p = Pattern.compile("\bis\b");
Matcher m = p.matcher(a);
while(m.find()){
System.out.println(a.substring(m.start(), m.end()));
}
}
Double escape it: 双逃避它:
Pattern p = Pattern.compile("\\bis\\b");
Regex in Java requires you to doubly escape certain special regex symbols, one escaping for Java and another for underlying regex engine. Java中的正则表达式要求您双重逃避某些特殊的正则表达式符号,一个用于Java,另一个用于底层正则表达式引擎。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.