用于自然语言处理的正则表达式“不包含”模式问题

问题描述

链接regex101

我正在实现一个简单的NLP算法。我已经实施了 解决方案遍历原始字符串以辅助正则表达式,但是现在我 想看看我是否可以在纯正则表达式中做到这一点。

我不知道如何让“构建”小组尊重负面的展望。我正在尝试捕获[“自然语言处理”算法],非常感谢任何帮助,谢谢?

$subject_string = <<<'subject_string'
Projects I've built & Plan to build. HackMatch.io (May 2020 onward),As of October 2020,I intend to start implementing "Natural Language Processing" algorithms 
in PHP when I have time. I'll then use PHP to upload the results to big data tech (e.g. BigQuery) 
to create some data visualizations.
subject_string;

$pattern = <<<'pattern'
/\b(?'verb'build|make|implementing)
(?'build'.+?(?!build|make|implementing)) 
(?=\bin\b|\bon\b)
(?:build|make|implementing)??/ix
pattern;

preg_match_all($pattern,$subject_string,$matches)

解决方法

您可以使用

/\b(?'verb'build|make|implementing)\s*
(?'build'(?:(?!(?&verb)).)*?) 
(?=\s*\b(?:in|on)\b)/ixs

请参见regex demo详细信息

  • \b-单词边界
  • (?'verb'build|make|implementing)-组“动词”:括号内的单词之一
  • \s*-zer或更多空白
  • (?'build'(?:(?!(?&verb)).)*?)-组“ build”:任何char,零个或多个出现,但尽可能少,不会启动“动词”组中定义的任何char序列
  • \s*-zer或更多空白
  • (?=\b(?:in|on)\b)-与位置相匹配的正向超前,紧跟着整个单词inon