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

发布于 2024-09-04 04:39:29 字数 698 浏览 6 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦断已成空 2024-09-11 04:39:29

这应该可以做到:

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

^.*selector=size.*$ 应该足够清晰。第一位 (?!.*details.cfm) 是负向前瞻:在匹配字符串之前,它会检查字符串不包含“details.cfm”(之前有任意数量的字符)它)。

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

许久 2024-09-11 04:39:29
^(?=.*selector=size)(?:(?!details\.cfm).)+$

如果您的正则表达式引擎支持所有格量​​词(尽管我怀疑 Google Analytics 不支持),那么我想这对于大型输入集会表现更好:

^[^?]*+(?<!details\.cfm).*?selector=size.*$
^(?=.*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.*$
谁对谁错谁最难过 2024-09-11 04:39:29

正则表达式可以是(perl 语法):

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

regex could be (perl syntax):

`/^[(^(?!.*details\.cfm).*selector=size.*)|(selector=size.*^(?!.*details\.cfm).*)]$/`
空‖城人不在 2024-09-11 04:39:29

接受的答案中的正则表达式存在问题。它也匹配 abcselector=sizeselector=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:

enter image description here

迷途知返 2024-09-11 04:39:29

我一直在寻找一种方法来避免在类似的情况下在尾部出现 --line-buffered ,因为 OP 和 Kobi 的解决方案对我来说非常有用。就我而言,排除带有“bot”或“spider”的行,同时包含 ' / ' (对于我的根文档)。

我原来的命令:

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文