简体   繁体   English

正则表达式 - 如何匹配除严格的单词之外的所有单词?

[英]Regexp - How to match all words except strict ones?

I'm trying to find a regexp that exclude specific word but not words containing it. 我试图找到一个排除特定单词的正则表达式,但不包含包含它的单词。

For example if we exclude the word "home" 例如,如果我们排除“家”这个词

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

Any ideas ? 有任何想法吗 ?

Thanks 谢谢

I would recommend taking a look at Rexegg — Word Boundaries . 我建议你看看Rexegg - Word Boundaries

If you want to match lines not containing "home" itself, you can do: 如果你想匹配不包含“home”本身的行,你可以这样做:

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

If you want to match the words alone: 如果你想单独匹配单词:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM