【问题标题】:Regexp - How to match all words except strict ones?Regexp - 如何匹配除严格单词之外的所有单词?
【发布时间】:2015-07-30 16:48:57
【问题描述】:

我正在尝试找到一个排除特定单词但不包含它的单词的正则表达式。

例如,如果我们排除“家”这个词

home --> NOT OK
homefree -> OK
freehome -> OK

有什么想法吗?

谢谢

【问题讨论】:

    标签: php regex codeigniter regex-negation codeigniter-routing


    【解决方案1】:

    我建议您查看Rexegg — Word Boundaries

    如果你想匹配不包含“home”本身的行,你可以这样做:

    preg_match_all('/^(?!.*\bhome\b).*$/im', $str, $matches);
    

    如果你想单独匹配单词:

    preg_match_all('/\b(?!home\b)\w+/i', $str, $matches);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多