【问题标题】:Regex "does not contain" pattern issue for Natural Language Processing自然语言处理的正则表达式“不包含”模式问题
【发布时间】:2021-01-31 17:48:06
【问题描述】:

链接到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)

【问题讨论】:

    标签: php regex algorithm artificial-intelligence pcre


    【解决方案1】:

    你可以使用

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

    请参阅regex demo详情

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

    【讨论】:

      猜你喜欢
      • 2015-08-20
      • 2014-06-04
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多