繁体   English   中英

如何简化此正则表达式以在Google Analytics(分析)中使用

[英]How to simplify this regular expression to use in Google Analytics

内容:Google Analytics(分析)

需要:一个采用给定URI或URN(是URN)的过滤器,它将返回所有内容,直至排除查询字符串。

如您所见,这里有多种变化,我希望我已经用下面的列表完整介绍了这些变化:

https://sub.domain.com/path/folder/article?l=en >> expected     https://sub.domain.com/path/folder/article
https://sub.domain.com/path/folder/103#3173l=en >> expected     https://sub.domain.com/path/folder/103   
https://sub.domain.com/path/folder/103?#3173l=en >> expected     https://sub.domain.com/path/folder/103
https://sub.domain.com/path/folder/103#?3173l=en
0sub.domain.tld  >> expected sub.domain.tld
sub.domain.tld/  >> expected sub.domain.tld
sub.domain.tld?param=value  >> expected sub.domain.tld
sub.domain.tld/?param=value  >> expected sub.domain.tld
sub.domain.tld?param=value#id  >> expected sub.domain.tld
sub.domain.tld/?param=value#id  >> expected sub.domain.tld
sub.domain.tld/folder  >> expected sub.domain.tld/folder
sub.domain.tld/folder/  >> expected sub.domain.tld/folder
sub.domain.tld/folder?param=value  >> expected   sub.domain.tld/folder
sub.domain.tld/folder/?param=value  >> expected  sub.domain.tld/folder
sub.domain.tld/1/folder  >> expected      sub.domain.tld/1/folder
sub.domain.tld/1/folder/  >> expected     sub.domain.tld/1/folder
2sub.domain.tld/1/folder?param=value
3sub.domain.tld/1/folder/?param=value
4sub.domain.tld#id
5sub.domain.tld/#id
6sub.domain.tld/1#id
7sub.domain.tld/1/#id

我无法解决的难题是获得一个正则表达式,该正则表达式与始终相同的子组中的事物匹配。

如果你要玩的时候,我有救了几个测试中- https://regex101.com/r/trZl06/1/ - https://regex101.com/r/SetgFn/2

后者在捕获我的案件方面非常令人满意,但是一旦在现有匹配条件之前添加了一个捕获组,该组甚至会收集到不期望的单词。

我也尝试过类似((.*)(?:[\\/]\\?.*)|(.*)(?:\\?.*))|((.*)\\/$|(.*))但生成的子组始终不同,从而使过滤器视图中的引用有些混乱。

您能想到什么吗?

您可以使用

^([^#?]*?)([/?#]?\?.*|[/#]?#.*)?(/?)$

参见regex演示

细节

  • ^ -字符串开头
  • ([^#?]*?) -组1: #?以外的0个或更多字符 ,尽可能少
  • ([/?#]?\\?.*|[/#]?#.*)? -可选的第2组:两者之一:
    • [/?#]?\\?.* -一个可选的/? #后跟一个? char然后是字符串的其余部分
    • | - 要么
    • [/#]?#.* -一个可选的/# ,随后用#焦炭,然后将字符串的其余部分
  • (/?) -第3组:可选的/
  • $ -字符串结尾。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM