Regular expression for a string containing one word but not another Ask Question

Regular expression for a string containing one word but not another Ask Question

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!

ベストアンサー1

This should do it:

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

^.*selector=size.*$十分に明らかであるはずです。最初の部分は(?!.*details.cfm)否定の先読みです。文字列を照合する前に、文字列に「details.cfm」(その前に任意の数の文字がある) が含まれていないかどうかを確認します。

おすすめ記事