regex - 正则表达式-匹配包含以下单词中的1个或2个但并非全部3个的URL

标签 regex google-analytics

我正在为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/

相关文章:

c# - 正则表达式匹配以句点开头并以下划线结尾的单词?

google-analytics - 如何在Blogger博客中添加Google Analytics(分析)?

google-analytics - 如何通过使用google adwords查看广告来导出安装了我的应用的用户的用户ID之类的数据?我想将其导出到大查询

sql - T-SQL正则表达式替换

java - 谷歌分析跟踪代码移动

javascript - URL 中带有日期的书签

javascript - 弹出窗口不适用于 UA 和 GTM

java - 正则表达式打印出里面的所有匹配项?和 , 或 )

python - 更改youtube/dailymotion/vimeo嵌入大小

java split regex - 任意字符组合