Search code examples
ruby-on-railsrubyregexrubular

Ruby Regex: Rejecting selected URLs


A survey software I'm using allows to exclude sites on which the survey should be shown using Ruby based Regex (they recommend testing strings on rubular.com). I don't want to show the survey to clients that are close to finishing transaction, so excluding 3 phrases makes more sense to me than including all the rest.

How would I have to approach writing a ruby regex string that includes everything except phrases cart, order and login within the URL?


Solution

  • The following ruby code rejects all strings in the array containing cart, order or login.

    urls.reject { |url| url[/(cart|order|login)/] }
    

    A raw regular expression which excludes words will use negative look-arounds:

    ^((?!login|order|cart).)*$
    

    See rubular.

    For more information, see @Max's suggested reading at Regular expression to match a line that doesn't contain a word?