I have a regex problem.

In google analytics, I want to add a filter that re-writers certain parts of URLs, and keeps only the last part of them.

My problem is that in these URLs, I have the character -

This is an example :


Here, i'm supposed to keep default

As you can see, the URL contains dashes -

So when I use the following regex :


It should give me the string default

Instead, it gives me fault

It cuts off the first 2 characters of the word. Anyone knows the reason?

I tried many solutions that didn't work exactly as I wanted :

[^/]+$ : This takes all URLs but I only want the URLs that start with the prefix above.

Any help would be appreciated

  • is it that you want to match only the part after the last forward-slash? – SanV Apr 11 at 14:59
  • @SanV I want to match the last part after the string /on/wordsearched.store/sites-eu-site/section_1/ – HelpASisterOut Apr 11 at 15:04
  • see my answer below for regex and .split() option to get what you want. – SanV Apr 11 at 16:01

The reason is because you use a negated character class starting with ^ inside the square brackets which matches not what is listed.

The first match from that character class is the f. After that match you capture all characters except a newline in a capturing group given you ault. So the whole match is fault

See the regex match

I think you meant to match default in the first capturing group where the ^ now means and anchor to assert the start of the string.


Regex demo

  • Thank you. For some reason this regex on Google Analytics filter kept me the FIrst part of the URL and deleted the last part (the one i want). For this /on/wordsearched.store/sites-eu-site/section_1/default it returned /on/wordsearched.store/sites-eu-site/section_1/ instead of default – HelpASisterOut Apr 19 at 9:10
  • @HelpASisterOut Try switching it around ^(\/on/wordsearched\.store\/sites-eu-site\/section_1\/).* regex101.com/r/oSNM4S/3 – The fourth bird Apr 19 at 9:12

This matches any word characters** after the last forward-slash:



** any letter, digit or underscore, equivalent to [a-zA-Z0-9_]

if google-analytics lets you use python code, you could simply use .split() as follows:

S = '/on/wordsearched.store/sites-eu-site/section_1/default'
S.split('/')[-1]  ## Output:  'default'

Will do a noncapture match for wordsearched and get the last / value

  • Thank you for your answer. The part with section is a variable, the only constant is the part with wordsearched – HelpASisterOut Apr 12 at 8:35
  • @HelpASisterOut oh in that case I've altered the regex to match by wordsearched and get by the last value in the string but you do have to refer to the target value by the group name last – BugCatcherJoe Apr 12 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.