gpt4 book ai didi

java - 正则表达式 : How to add list as condition to satisfy regex in java

转载 作者:行者123 更新时间:2023-11-30 07:55:42 26 4
gpt4 key购买 nike

我想检查下一个句子是否以wordList中已列出的单词开头?

WordList = {Hello, Namshte, Hi, Hey ...... around 500 records- dummy list here }

例如。这是第一句话。这是第二名。 你好,这是我的第三句话。

Requirement ->  "." + " " (space) + "<word from List>" 

对于简单的正则表达式,我可以写“\.\s[A-Z]”以字母开头。但我想检测以列表中的单词开头的句子。

REGEX -> \.\s[???]   -> ? how to add List here

如何正确选择第三句?

最佳答案

使用此正则表达式,以 Java 字符串形式给出:

"\\.\\s(?i:Hello|Namshte|Hi|Hey)\\b"

说明:

  • \\. 比赛周期
  • \\s 匹配空格
  • (?i: ) 非捕获组匹配,不区分大小写。
  • Hello|Namshte|Hi|Hey 匹配其中一个单词。
  • \\b 匹配单词边界以防止单词匹配,例如 Hijack

要选择整个第三句,即直到并包括下一个句点,请使用以下命令:

"\\.\\s((?i:Hello|Namshte|Hi|Hey)\\b[^.]+\\.)"

捕获组是句子。

更新代码示例:

String[] wordList = { "Hello", "Namshte", "Hi", "Hey", ...... 500 words };

StringBuilder buf = new StringBuilder();
for (String word : wordList) {
if (buf.length() != 0)
buf.append('|');
buf.append(Pattern.quote(word));
}
Pattern regex = Pattern.compile("\\.\\s((?i:" + buf + ")\\b[^.]+\\.)");

String text = "This is 1st Sentence. This is 2nd place. Hello,This is my 3rd Sentence." +
" This is 4th place. Namshte, at 5.";
Matcher m = regex.matcher(text);
while (m.find())
System.out.println("Found: " + m.group(1));

输出

Found: Hello,This is my 3rd Sentence.
Found: Namshte, at 5.

关于java - 正则表达式 : How to add list as condition to satisfy regex in java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32710852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com