Regular expression for a string containing one word but not another

牧云@^-^@ 提交于 2019-12-16 19:46:11

问题


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.*$ 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).




回答2:


regex could be (perl syntax):

`/^[(^(?!.*details\.cfm).*selector=size.*)|(selector=size.*^(?!.*details\.cfm).*)]$/`



回答3:


^(?=.*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.*$



回答4:


I was looking for a way to avoid --line-buffered on a tail in a similar situation as the OP and Kobi's solution works great for me. In my case excluding lines with either "bot" or "spider" while including ' / ' (for my root document).

My original command:

tail -f mylogfile | grep --line-buffered -v 'bot\|spider' | grep ' / '

Now becomes (with "-P" perl switch):

tail -f mylogfile | grep -P '^(?!.*(bot|spider)).*\s\/\s.*$'


来源:https://stackoverflow.com/questions/2953039/regular-expression-for-a-string-containing-one-word-but-not-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!