Regex matches a 2-level URL, not a 3-level URL (Google Analytics)

I am trying to create content groups in Google Analytics. How to combine a page with sports / [SOMETHING], not sports / [SOMETHING] / [SOMETHING]

Match:

  • / sports / football
  • / sport / men-basketball
  • / sports / baseball

Don't match:

  • / sport / men's-basketball / standings
  • / sport / football / standings
  • / sports / football / scores
+4


source to share


2 answers


We can say "not following characters" by using [^...]

^

means "not". Therefore, to limit the levels, we can use [^/]

.

Here are some examples:

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



One must start with / and then exactly 1 / must be followed: ^/[^/]+/[^/]+$

or^(/[^/]+){2}$

Generalized to start with / and follow 4 / no more: ^(/[^/]+){1,5}$

+4


source


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

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



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

/ $ => page depth of the first closing level / s $ stop

0


source







All Articles