Ghilas BELHADJ Ghilas BELHADJ - 1 year ago 67
Ruby Question

Match string that doesn't contain a specific word

I'm working with ruby with the

method and I want to match an URL that doesn't contain a certain string with a regular Expression:

I want to match the URLs that doesn't contain the word
, so the 1st and the 2nd ones should be matched

Answer Source

Just use a negative lookahead ^(?!.*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

Online demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download