我正在为Google Analytics(分析)建立正则表达式,并且已经准备就绪,但是我只停留在最后一部分。
我试图匹配URL中的特定单词,而不考虑其顺序,但是我想排除同时包含3个特定单词的URL。
这是4个网址:
/find-store?radius=30&manufacturers=sony,phillips,magnavox&segment=residential&postal=998028#
/find-store?search=Juneau%2C+AK+99802%2C+USA&radius=30&manufacturers=sony,magnavox&segment=commercial&postal=998028#
/find-store?radius=30&manufacturers=phillips,sony&segment=residential&postal=998028#
/find-store?radius=30&manufacturers=magnavox&segment=residential&postal=998028#
我希望我的正则表达式匹配上述所有URL,但第一个URL(包含sony,phillips和magnavox)除外。品牌的顺序可以不同,因此无论顺序如何,都需要检查这三个词是否存在。
这是我当前的正则表达式,与所有这些URL匹配:
(find-store.*sony.*magnavox)|(find-store.*sony.*phillips)|(find-store.*sony)
最佳答案
此正则表达式有效。 ^(?!(?=.*sony)(?=.*phillips)(?=.*magnavox)).+$
^ # BOS
(?! # Cannot be all three on the line
(?= .* sony )
(?= .* phillips )
(?= .* magnavox )
)
.+
$ # EOS
对于特定短语
^(?!(?=.*sony)(?=.*phillips)(?=.*magnavox)).*find-store.*$
^ # BOS
(?! # Cannot be all three on the line
(?= .* sony )
(?= .* phillips )
(?= .* magnavox )
)
.*
find-store # Add sepcific phrase/words
.*
$ # EOS
您也可以将特定短语放在顶部
# ^.*?find-store(?!(?=.*sony)(?=.*phillips)(?=.*magnavox)).+$
^ # BOS
.*?
find-store # Add sepcific phrase/words
(?! # Cannot be all three on the line
(?= .* sony )
(?= .* phillips )
(?= .* magnavox )
)
.+
$ # EOS
如果您需要索尼,菲利普斯或magnovox,则可以在底部添加它们。
# ^.*?find-store(?!(?=.*sony)(?=.*phillips)(?=.*magnavox)).*?(sony|phillips|magnavox).*?$
^ # BOS
.*?
find-store # Add required sepcific phrase/words
(?! # Cannot be all three on the line
(?= .* sony )
(?= .* phillips )
(?= .* magnavox )
)
.*?
( sony | phillips | magnavox ) # (1), Required. one of these
.*?
$ # EOS
关于regex - 正则表达式-匹配包含以下单词中的1个或2个但并非全部3个的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28222342/