我正在使用 ruby 和 match
方法,我想用正则表达式匹配一个不包含特定字符串的 URL:
例如:
http://website1.com/url_with_some_words.html
http://website2.com/url_with_some_other_words.html
http://website3.com/url_with_the_word_dog.html
我想匹配不包含单词dog
的URL,所以应该匹配第一个和第二个
最佳答案
只需使用否定前瞻 ^(?!.*dog).*$
。
解释
^
: 匹配行首(?!.*dog)
:否定前瞻,检查单词 dog 是否不存在.*
:匹配所有内容(本例中的换行符除外)$
: 匹配行尾
关于ruby - 匹配不包含特定单词的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11653942/