Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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
like image 757
Hung Luu Avatar asked Jul 28 '15 23:07

Hung Luu


1 Answers

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

like image 62
maraca Avatar answered Nov 14 '22 20:11

maraca