Regular expression for a string containing one word but not another

114,963

Solution 1

This should do it:

^(?!.*details\.cfm).*selector=size.*$

^.*selector=size.*$ should be clear enough. The first bit, (?!.*details.cfm) is a negative look-ahead: before matching the string it checks the string does not contain "details.cfm" (with any number of characters before it).

Solution 2

^(?=.*selector=size)(?:(?!details\.cfm).)+$

If your regex engine supported posessive quantifiers (though I suspect Google Analytics does not), then I guess this will perform better for large input sets:

^[^?]*+(?<!details\.cfm).*?selector=size.*$

Solution 3

regex could be (perl syntax):

`/^[(^(?!.*details\.cfm).*selector=size.*)|(selector=size.*^(?!.*details\.cfm).*)]$/`
Share:
114,963
Chris Stahl
Author by

Chris Stahl

Updated on March 31, 2021

Comments

  • Chris Stahl
    Chris Stahl about 3 years

    I'm setting up some goals in Google Analytics and could use a little regex help.

    Lets say I have 4 URLs

    http://www.anydotcom.com/test/search.cfm?metric=blah&selector=size&value=1
    http://www.anydotcom.com/test/search.cfm?metric=blah2&selector=style&value=1
    http://www.anydotcom.com/test/search.cfm?metric=blah3&selector=size&value=1
    http://www.anydotcom.com/test/details.cfm?metric=blah&selector=size&value=1
    

    I want to create an expression that will identify any URL that contains the string selector=size but does NOT contain details.cfm

    I know that to find a string that does NOT contain another string I can use this expression:

    (^((?!details.cfm).)*$)
    

    But, I'm not sure how to add in the selector=size portion.

    Any help would be greatly appreciated!

  • Kobi
    Kobi almost 14 years
    This assumes selector=size is always before details.cfm, which isn't the case in the last url.
  • Kobi
    Kobi almost 14 years
    Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
  • Tomalak
    Tomalak almost 14 years
    @Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
  • Joshua Pinter
    Joshua Pinter about 10 years
    FYI, check out regexr.com for a nice way to test these expressions out.
  • Alexei Blue
    Alexei Blue about 6 years
    Always forget about negative lookahead and it's so useful
  • Cary Swoveland
    Cary Swoveland over 5 years
    "http://www.anydotcom.com/test/search.cfm?metric=blah&select‌​or=sized&value=1" =~ /^(?!.*details\.cfm).*selector=size.*$/ #=> 0 is incorrect. (Note the string contains "...selector=sized...".) Also, why .*$ at the end?
  • Wiktor Stribiżew
    Wiktor Stribiżew almost 5 years
    This is a corrupted regex, the square brackets turn all the pattern sequences into combination of individual chars.
  • Chris Stahl
    Chris Stahl about 3 years
    While you are not wrong, the regex as originally accepted met my need as your examples did not exist in the set of possible strings.