Regular expression for a string containing one word but not another

后端 未结 4 1993
孤独总比滥情好
孤独总比滥情好 2020-11-22 02:27

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?met         


        
相关标签:
4条回答
  • 2020-11-22 02:39
    ^(?=.*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.*$
    
    0 讨论(0)
  • 2020-11-22 02:54

    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.*$'
    
    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-11-22 02:57

    regex could be (perl syntax):

    `/^[(^(?!.*details\.cfm).*selector=size.*)|(selector=size.*^(?!.*details\.cfm).*)]$/`
    
    0 讨论(0)
提交回复
热议问题