如果字符串 "I am in the EU."
中存在一个完整的词,即 "EU"
,我将如何查找,同时不匹配像这样的情况“我在欧洲。”
?
基本上,我想要某种正则表达式来表示单词,即 “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");
}
}
关于java - 正则表达式查找整个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12574853/