为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕娘9325324 2022-12-29 16:05:16
我想过滤每句话的前四个词,第一个词是“This”,最后一个词是“on”。我一直在尝试观看 YouTube 教程,但我所能做的就是:([A-Z](?:[^\s.!?]+(?:\s|\n)){0,4}(?:[^\s.!?]+)?)现在,这里有一个例子:这个 [perception resids] on...
查看完整描述

3 回答

?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

你应该考虑使用一些 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


查看完整回答
反对 回复 2022-12-29
?
动漫人物

TA贡献1815条经验 获得超10个赞

最基本的正则表达式是

/\bThis\s+\w+\s+\w+\s+on\b/

这将匹配没有捕获。也许您认为的“单词”字符可能与正则表达式引擎认为的单词字符不同。


查看完整回答
反对 回复 2022-12-29
?
www说

TA贡献1775条经验 获得超8个赞

(?:^|[.;!?]\s+)(\bThis\W*?(\b\w+\b)\W*?(\b\w+\b)\W*on\b)

这样的事情会起作用吗?据我了解,您希望句子有四个词,以“This”开头,以“on”结尾。


查看完整回答
反对 回复 2022-12-29
  • 3 回答
  • 0 关注
  • 161 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号