匹配不包含特定单词的字符串

Ghi*_*ADJ 24 ruby regex match

我正在使用该match方法使用ruby,我希望将不包含某个字符串的URL与正则表达式匹配: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
Run Code Online (Sandbox Code Playgroud)

我想匹配不包含单词的网址dog,因此第一个和第二个匹配

Ham*_*mZa 43

只需使用否定前瞻^(?!.*dog).*$.

说明

  • ^ :匹配行的开头
  • (?!.*dog) :负向前瞻,检查单词dog是否不存在
  • .* :匹配所有内容(在这种情况下除了换行符)
  • $ :匹配行尾

在线演示


Ant*_*ton 9

只是用

string !~ /dog/
Run Code Online (Sandbox Code Playgroud)

选择你需要的字符串.