正则表达式匹配包含带句点的特定单词的字符串

Regular expression matching strings where it contains a specific word that has a period

我正在尝试编写一个正则表达式来匹配包含带句点的特定单词的字符串,例如(apple. 或 grape.)。我让它在没有句号的情况下工作,但不太确定当单词中有句号时如何让它工作。

我尝试了什么:

(?i)\b(Apple|Grape)\b (Working correctly without the period)
(?i)\b(Apple\.|Grape\.)\b (Returns no matches)

应该有效的示例字符串:

1 apple.
1 Apple.
apple. 2
grape. 1
test grape.
grape. test
this is a Apple. test

不应工作的示例字符串:

1apple.
1Apple.
apple.2
grape.1
testgrape.
grape.test
longwordApple.test
this is a Apple.test

您可以将模式写为:

\b(Apple|Grape)\.(?!\S)

说明

  • \b 一个单词边界以防止左侧出现部分单词匹配
  • (Apple|Grape) 捕获苹果或葡萄
  • \.匹配一个点
  • (?!\S) 断言右边的空白边界

Regex demo

在 Java 中使用双转义反斜杠:

String regex = "(?<!\S)(Apple|Grape)\.(?!\S)";

您确定需要正则表达式吗?我的意思是我可以在以下几行中想到一个普通的标记化解析器:

String s = "";
foreach(String token : s.split(" ")) { // I suggest saving the split (apparently, javac does not do a good job at caching like the C/C++ compiler)
    if(token.equals("apple.") || token.equals("grapes.")) { // or take in a word array with all matches and then run it over all those (n^2 complexity)
        
        //whatever you wanna do
    }
}