匹配不包含特定单词的字符串 [英] Match string that doesn't contain a specific word
本文介绍了匹配不包含特定单词的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用match
方法使用ruby,我想用正则表达式匹配不包含特定字符串的URL:
例如:
I'm working with ruby with the match
method and I want to match an URL that doesn't contain a certain string with a regular Expression:
ex:
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
的网址,因此第一个和第二个应该匹配
I want to match the URLs that doesn't contain the word dog
, so the 1st and the 2nd ones should be matched
推荐答案
只需使用否定的前瞻^(?!.*dog).*$
.
说明
-
^
:匹配行的开头 -
(?!.*dog)
:前瞻否定,请检查狗"一词是否不存在 -
.*
:匹配所有内容(本例中的换行符除外) -
$
:匹配行尾
^
: match begin of line(?!.*dog)
: negative lookahead, check if the word dog doesn't exist.*
: match everything (except newlines in this case)$
: match end of line
这篇关于匹配不包含特定单词的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文