包含一个词而不是另一个词的字符串的正则表达式

时间:2022-02-16 15:23:07

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

我在谷歌分析中建立了一些目标,可以使用一些regex帮助。

Lets say I have 4 URLs

假设我有4个url

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

我想创建一个表达式,该表达式将标识包含字符串选择器=size但不包含details.cfm的任何URL

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.

但是,我不知道如何添加selector=size部分。

Any help would be greatly appreciated!

如有任何帮助,我们将不胜感激!

5 个解决方案

#1


90  

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

^ *选择器=大小。*$应该足够清楚。第一个位(?!.*details.cfm)是一个负面的查找:在匹配字符串之前,它检查字符串是否包含“详细信息”。cfm"(前面有任意数量的字符)。

#2


5  

regex could be (perl syntax):

regex可以是(perl语法):

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

#3


1  

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

如果你的regex引擎支持后格的量词(尽管我怀疑谷歌分析没有),那么我想这对大型输入集的性能会更好:

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

#4


0  

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

我在寻找一种避免的方法——在类似的情况下用线缆缓冲机尾,就像OP和Kobi的解决方案对我很有效一样。在我的例子中,排除使用“bot”或“spider”的行,同时包含' / '(对于我的根文档)。

My original command:

我原来的命令:

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

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

现在变成(用“-P”perl开关):

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

#5


-4  

Simple way to do this is to specify 0 instances of the string by doing the following

实现此目的的简单方法是通过以下操作指定字符串的0个实例

(string_to_exclude){0}

#1


90  

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

^ *选择器=大小。*$应该足够清楚。第一个位(?!.*details.cfm)是一个负面的查找:在匹配字符串之前,它检查字符串是否包含“详细信息”。cfm"(前面有任意数量的字符)。

#2


5  

regex could be (perl syntax):

regex可以是(perl语法):

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

#3


1  

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

如果你的regex引擎支持后格的量词(尽管我怀疑谷歌分析没有),那么我想这对大型输入集的性能会更好:

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

#4


0  

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

我在寻找一种避免的方法——在类似的情况下用线缆缓冲机尾,就像OP和Kobi的解决方案对我很有效一样。在我的例子中,排除使用“bot”或“spider”的行,同时包含' / '(对于我的根文档)。

My original command:

我原来的命令:

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

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

现在变成(用“-P”perl开关):

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

#5


-4  

Simple way to do this is to specify 0 instances of the string by doing the following

实现此目的的简单方法是通过以下操作指定字符串的0个实例

(string_to_exclude){0}