Regex to match 2 level deep URL and not 3 level deep (Google Analytics)

时间:2015-07-28 23:20:48

标签: regex

I'm trying to create content groups in Google Analytics. How do I match a page with sport/[SOMETHING] and not sport/[SOMETHING]/[SOMETHING]

Match:

  • /sport/football
  • /sport/mens-basketball
  • /sport/baseball

Do not Match:

  • /sport/mens-basketball/standings
  • /sport/football/standings
  • /sport/football/scores

2 个答案:

答案 0 :(得分:5)

We can say "not the following characters" using [^...] the ^ stands for "not". So to restrict the levels we can use [^/].

Here some examples:

Match /sport/something : ^/sport/[^/]+$

Has to start with / and then exactly 1 / has to follow: ^/[^/]+/[^/]+$ or ^(/[^/]+){2}$

Generalized for start with / and followed by 4 / at most: ^(/[^/]+){1,5}$

答案 1 :(得分:1)

这对我有用Just "someguy"

说明: ^ / sports / =>类别,以匹配/使用\

转义

([[^ /] +)=>不是/一次或多次

/ $ =>结束的第一级页面深度/带有$止损