包含一个单词但不包含另一个单词的字符串的正则表达式
我正在 Google Analytics 中设置一些目标,并且可以使用一些正则表达式帮助。
假设我有 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
我想创建一个表达式来识别包含字符串 selector=size 但不包含 details.cfm 的任何 URL
我知道要查找不包含另一个字符串的字符串我可以使用这个表达式:
(^((?!details.cfm).)*$)
但是,我不确定如何添加 selector=size 部分。
任何帮助将不胜感激!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这应该可以做到:
^.*selector=size.*$
应该足够清晰。第一位(?!.*details.cfm)
是负向前瞻:在匹配字符串之前,它会检查字符串不包含“details.cfm”(之前有任意数量的字符)它)。This should do it:
^.*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).如果您的正则表达式引擎支持所有格量词(尽管我怀疑 Google Analytics 不支持),那么我想这对于大型输入集会表现更好:
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:
正则表达式可以是(perl 语法):
regex could be (perl syntax):
接受的答案中的正则表达式存在问题。它也匹配
abcselector=size
、selector=sizeabc
等。正确的正则表达式可以是
^(?!.*\bdetails\.cfm\b).*\bselector=size\b.*$
正则表达式的说明在 regex101:
There is a problem with the regex in the accepted answer. It also matches
abcselector=size
,selector=sizeabc
etc.A correct regex can be
^(?!.*\bdetails\.cfm\b).*\bselector=size\b.*$
Explanation of the regex at regex101:
我一直在寻找一种方法来避免在类似的情况下在尾部出现
--line-buffered
,因为 OP 和 Kobi 的解决方案对我来说非常有用。就我而言,排除带有“bot”或“spider”的行,同时包含' / '
(对于我的根文档)。我原来的命令:
现在变成(使用
-P
perl 开关):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:
Now becomes (with
-P
perl switch):