每个句子的前四个单词,但必须以单词 X 开头,以单词 Y 结尾

我想过滤每句话的前四个词,第一个词是“This”,最后一个词是“on”。我一直在尝试观看 YouTube 教程,但我所能做的就是:

([A-Z](?:[^\s.!?]+(?:\s|\n)){0,4}(?:[^\s.!?]+)?)

现在,这里有一个例子:这个 [perception resids] on...


慕娘9325324
浏览 162回答 3
3回答

鸿蒙传说

你应该考虑使用一些 NLP 包将文本拆分成句子。然后使用^This\s+\S+\s+\S+\s+on\b它匹配一个以 wth 开头的字符串This,然后有两个包含任何非空白字符的单词,然后是单词on。见证明解释NODE                     EXPLANATION--------------------------------------------------------------------------------  ^                        the beginning of the string--------------------------------------------------------------------------------  This                     'This'--------------------------------------------------------------------------------  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or                           more times (matching the most amount                           possible))--------------------------------------------------------------------------------  \S+                      non-whitespace (all but \n, \r, \t, \f,                           and " ") (1 or more times (matching the                           most amount possible))--------------------------------------------------------------------------------  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or                           more times (matching the most amount                           possible))--------------------------------------------------------------------------------  \S+                      non-whitespace (all but \n, \r, \t, \f,                           and " ") (1 or more times (matching the                           most amount possible))--------------------------------------------------------------------------------  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or                           more times (matching the most amount                           possible))--------------------------------------------------------------------------------  on                       'on'--------------------------------------------------------------------------------  \b                       the boundary between a word char (\w) and                           something that is not a word char

动漫人物

最基本的正则表达式是/\bThis\s+\w+\s+\w+\s+on\b/这将匹配没有捕获。也许您认为的“单词”字符可能与正则表达式引擎认为的单词字符不同。

www说

(?:^|[.;!?]\s+)(\bThis\W*?(\b\w+\b)\W*?(\b\w+\b)\W*on\b)这样的事情会起作用吗?据我了解,您希望句子有四个词,以“This”开头,以“on”结尾。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript