如何在字符串"I am in the EU."
内存在整个单词(即"EU"
),而不是像"I am in Europe."
这样的匹配案例?正则表达式找到整个单词
基本上,我想要某种形式的正则表达式,即"EU"
两边都有非字母字符。
如何在字符串"I am in the EU."
内存在整个单词(即"EU"
),而不是像"I am in Europe."
这样的匹配案例?正则表达式找到整个单词
基本上,我想要某种形式的正则表达式,即"EU"
两边都有非字母字符。
.*\bEU\b.*
public static void main(String[] args) {
String regex = ".*\\bEU\\b.*";
String text = "EU is an acronym for EUROPE";
//String text = "EULA should not match";
if(text.matches(regex)) {
System.out.println("It matches");
} else {
System.out.println("Doesn't match");
}
}
你可以做类似
String str = "I am in the EU.";
Matcher matcher = Pattern.compile("\\bEU\\b").matcher(str);
if (matcher.find()) {
System.out.println("Found word EU");
}
使用模式与字边界:
String str = "I am in the EU.";
if (str.matches(".*\\bEU\\b.*"))
doSomething();
看一看的docs for Pattern
。 。
看看单词边界\ b – gtgaxiola