0

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?

1
  • 1
    regex's aren't very good at matches like "everything that doesn't contain a particular word". But this question might be useful stackoverflow.com/questions/406230/… Commented May 19, 2014 at 11:45

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

6 Comments

Rubular notified me of escaping forward slashes, which I added to your string, but then it still didnt match 1 of 4 testing URLs that didnt have any of the aforementioned phrases.
The forward slashes are the ruby syntax for regular expressions, much like quotes are for strings. Also, using reject throws away any matching URLs, and keeps only those which do not match.
Matt, please could you share the failing testing URL?
Sure, I'm still reading various materials regarding negative lookahead, but can't seem to write such a string. Here is Uri's string with my testing URL's.
@Matt - we are having some misunderstanding - what I posted is ruby code, not a standalone regular expression. The raw regular expression is (cart|order|login), which selects all the URLs which contain cart,order or login.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.