5

我将如何找到整个单词,即"EU",是否存在于 String"I am in the EU."中,而不匹配大小写,例如"I am in Europe."

基本上,我想要某个词的正则表达式,即"EU"两边都有非字母字符。

4

3 回答 3

8

.*\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");
       }

    }
于 2012-09-25T00:55:46.527 回答
4

你可以做类似的事情

String str = "I am in the EU.";
Matcher matcher = Pattern.compile("\\bEU\\b").matcher(str);
if (matcher.find()) {
   System.out.println("Found word EU");
}
于 2012-09-25T00:57:50.643 回答
3

使用带有单词边界的模式:

String str = "I am in the EU.";

if (str.matches(".*\\bEU\\b.*"))
    doSomething();

查看. _Pattern

于 2012-09-25T00:58:02.000 回答