Home > Software design >  Catch URL with parameters or end of URL using regular expression
Catch URL with parameters or end of URL using regular expression

Time:12-11

I'm trying to figure out how to capture some URL patterns with regular expression in Google Analytics, but I have not yet figured out how.

Here are some example URLs:

url1: myurl.com/dir1/dir2

url2: myurl.com/dir1/dir2?myparam=1

url3: myurl.com/dir2/dir2/anotherdir/

I would like to be able to capture the two first ones but not the last one and then the last one but not the two first ones.

I tried for example: (myurl.com/dir1/dir2$|myurl.com/dir1/dir2?) -> I get url1 but not url2

myurl.com/dir1[^/] -> doesn't work

myurl.com/dir1? -> doesn't work

myurl.com/dir1/ -> capture url 3

So capturing url3 only is easy but I don't see how to capture url1 and url2.

CodePudding user response:

If I understand correctly, can't you just use this for the first two:

myurl\.com\/dir1\/dir2($|\?. )

And use this for the last:

myurl\.com\/dir2\/dir2\/.

I noticed that you are not using any escape characters. You should escape reserved characters with backslash.

You can use https://regex101.com/ to get detailed explanation.

  • Related