gpt4 book ai didi

java - 正则表达式从段落中查找包含特定单词(java)的句子

转载 作者:行者123 更新时间:2023-11-29 06:38:42 25 4
gpt4 key购买 nike

我有一个单词列表:dogcatleopard

我试图在 Java 中提出一个正则表达式,以从包含任何一个单词(不区分大小写)的长段落中提取句子。句子以 . ?! 结尾有人能帮忙吗?谢谢你!

最佳答案

假设

  • 句子必须以大写字母开头,中间没有行终止符 [.?!]。
  • 关键字匹配不区分大小写。但是,子字符串匹配无效。
  • 关键字可以出现在句子的任何位置(开头、结尾或中间)。
  • 支持引号和非正式双标点符号。如果不需要,请使用第二个正则表达式。

public class SentenceFinder {

public static void main(String[] args) {
String paragraph = "I have a list of words to match: dog, cat, leopard. But blackdog or catwoman shouldn't match. Dog may bark at the start! Is that meow at the end my cat? Some bonus sentence matches shouldn't hurt. My dog gets jumpy at times and behaves super excited!! My cat sees my goofy dog and thinks WTF?! Leopard likes to quote, \"I'm telling you these Lions suck bro!\" Sometimes the dog asks too, \"Cat got your tongue?!\"";
Pattern p = Pattern.compile("([A-Z][^.?!]*?)?(?<!\\w)(?i)(dog|cat|leopard)(?!\\w)[^.?!]*?[.?!]{1,2}\"?");
Matcher m = p.matcher(paragraph);
while (m.find()) {
System.out.println(m.group());
}
}
/* Output:
I have a list of words to match: dog, cat, leopard.
Dog may bark at the start!
Is that meow at the end my cat?
My dog gets jumpy at times and behaves super excited!!
My cat sees my goofy dog and thinks WTF?!
Leopard likes to quote, "I'm telling you these Lions suck bro!"
Sometimes the dog asks too, "Cat got your tongue?!"
*/
}


简化的正则表达式,如果“引号?!” (或非正式标点符号)不是必需的:
"([A-Z][^.?!]*?)?(?<!\\w)(?i)(dog|cat|leopard)(?!\\w)[^.?!]*?[.?!]"

要获取那些不以大写字母开头的句子(如果输入可能有此类拼写错误):
"(?i)([a-z][^.?!]*?)?(?<!\\w)(dog|cat|leopard)(?!\\w)[^.?!]*?[.?!]"

关于java - 正则表达式从段落中查找包含特定单词(java)的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15990083/

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