简体   繁体   中英

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

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

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}$

This worked for me ^\\/sports\\/([^\\/]+)\\/$

Explanation: ^/sports/ => category to match with / escaped using \\

([^/]+) => the not / once or more

/$ => the closing first level page depth / with a $ stop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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