Regular expressions, at first they seem daunting. You realize how powerful they are once you get used to them. It might take a while, but it is definitely worth it!
They are useful for both marketers as well as more technically oriented people. You can’t go without regular expressions when auditing and setting up Google Analytics accounts and reports.
This guide clearly explains how to use regular expressions in Google Analytics. It includes everything you need to know te become a Google Analytics Regular Expression (RegEx) Master!
Table of Contents
- Overview of Regular Expressions in Google Analytics
- Five Effective Ways to Use Google Analytics RegEx
- Regular Expression Tester
- Regular Expression Cheat Sheet
- Regular Expressions and Google Analytics API
Further, I’ve put together a handy RegEx Cheat Sheet which you can keep at your desk!
Overview of Regular Expressions
There are 13 regular expressions in Google Analytics. This includes combinations of the most common regular expressions.
Some of them you will rarely use, others maybe on a daily basis!
I have included many Google Analytics RegEx examples to make this guide both practical as well as actionable!
The regular expressions that I use most often are on top of the list. Have fun exploring them!
Pipe (|)
The pipe symbol is the simplest one and means or.
An example:
In this case I tried to match two pages: /ebooks/ and /tools/.
Please note that pages that contain these subdirectories match as well. Later you will learn how to be more precise when using regular expressions.
Dot (.)
A dot matches any character. It’s like a wildcard.
So you could use it in the expression .ook. In this case it would match book, took, look, cook etc., but not ook.
The dot equals one character.
Note: the power of this RegEx lies in using it together with other RegEx characters.
Asterisk (*)
The asterisk means match zero or more of the previous item.
An example:
– boo*ks -> it matches boks, books, boooks, booooks etc.
Note: the power of this RegEx lies in using it together with the dot RegEx.
Dot-Asterisk (.*)
The dot-asterisk is definitely a powerful combination!
It matches zero or more random characters. In other words it matches everything.
There are many instances in which you would like to use this combination.
Check out the following filter:
I have put parentheses around the .*, like this: (.*) This means get all characters and put them in a variable. So we get the entire hostname and the entire request URI in a variable, and then in the bottom field I combine both variables.
By doing this your full URL will show up in Google Analytics.
Tip: read this article about filters in Google Analytics.
Another example to make things clear to you. Let’s assume you are running a website and sell bicycles to men, women and kids. These are the three categories on the website:
- /products/men/cycles/
- /products/women/cycles/
- /products/kids/cycles/
You could use /products/.*/cycles/ to match all three categories.
Note: keep in mind that the processing time of this RegEx is quite long. So don’t misabuse it!
Backslash (\)
The backslash RegEx is very useful and one of the regular expressions you should definitely use.
In my experience you will use this one a lot.
They turn special (RegEx) characters into normal characters.
Two examples:
- Request URI = /gp/product/B009TGWVRG/ref=s9_nwrsa_gw_g318_i3\?pf_rd_m=ATVP
- IP address = 67\.172\.171\.105
The first example is based on an Amazon url. You can see that I used a backslash to “escape” the question mark. By doing this I turn it into a normal character. There are a lot of urls that contain query parameters so this one might come in handy!
The second example is based on an IP address that contains three dots. We learned that a dot means a random character (RegEx). It’s better to escape it here since it should be read as a plain, normal character.
Caret (^)
The caret has a lot of value as well. It means that something begins with…
An example:
^shoe -> It matches shoe, shoes, shoes for winter, but it doesn’t match winter shoe or winter shoes.
Dollar sign ($)
The dollar sign is easy to understand now you know how the caret works.
It means that something ends with…
An example:
shoe$ -> It matches shoe, winter shoe, but it doesn’t match winter shoes or winter shoe guide.
Question mark (?)
A question mark means the last character is optional.
In general this one it useful for targeting misspellings.
Let’s assume that Stefan is the CEO of a company called Reggex. This company is running a pay per click campaign and likes to filter out all brand searches on Stefan and the company name.
Here is a smart way to do that:
This way all pay per click keywords that contain stefan, steffan, reggex and regex are included. You will be astonished how often these type of names are misspelled.
Parentheses ()
I love using parentheses. Actually, they work in the same way as in mathematics.
Let me show this by two examples:
- 2 x 7 +13 = 27
- 2 x (7 + 13) = 40
By using the parenteses you group two numbers together before you do the calculation.
I have already showed these directories:
- /products/men/cycles/
- /products/women/cycles/
- /products/kids/cycles/
You have learned you can use .* to match anything.
If you want to make a 100% match, you could use the following regular expression:
^/products/(men|women|kids)/cycles/$
Now we are getting somewhere!
- The request URI starts with /products and ends with cycles/.
- The middle directory contains either men, women or kids
The more you know about Google Analytics RegEx, the faster and more accurate you can work.
We are almost there, three more to go.
Square brackets ([])
The square brackets help you to make a simple list.
For example [aeo]. Combined with other characters t[aeo]p. It matches tap, tep and top.
Tip: use them together with dashes to create a powerful list.
Dashes (-)
The dashes are a great help to create a (more advanced) list of items.
It is a best practice to use them together with square brackets.
- [a-z] matches all lower-case letters
- [A-Z] matches all upper-case letters
- [0-9] matches all numbers
- [a-zA-Z0-9] matches all lower-case and upper-case letters and numbers
An example:
Jake is product manager of Nike Air Max Shoes and he is eager to sell more!
You want to monitor this year’s shoes but also a few legacy editions:
- Nike Air Max 2012
- Nike Air Max 2013
- Nike Air Max 2014
- Nike Air Max 2015
Google Analytics can filters these products in an easy way:
Another RegEx that would work in this situation:
Nike Air Max 201(2|3|4|5)
They both match the four editions and Jake is happy to monitor the product line perfomance in an easy way! :-)
Plus sign (+)
The plus sign matches one or more of the previous characters.
I use it on a rarely basis, but it is good to know this one exists!
An example:
hello+ matches hello, helloo, hellooo, helloooo (you got the point now :-)).
Curly brackets ({ })
We made it to the final one!
It’s probably not the most easy one to explain, so I will talk about this with the help of two examples:
- {1,2} – it means, repeat the last “item” at least 1 times and no more than 2 times.
- {2} – it means, repeat the last “item” 2 times
I have used the first one in RegEx IP ranges.
An example:
77.120.120.0 to 77.120.120.99 -> RegEx would look like ^77\.120\.120\.[0–9]{1,2}$
The second one I have rarely used, but an example with ZIP codes:
12[0–9]{3} would match 12xxx. First two numbers of the ZIP code are 1 and 2 followed by three random numbers in the range of 0 to 9.
Five Effective Ways to Use Google Analytics RegEx
By now I hope you agree with me that regular expressions are very effective in Google Analytics.
To convince you even more, I will explain five situations where you really want to use RegEx.
1. Applying Table Filters
It has not always been that way, but happily it is now allowed to use RegEx in table filters.
This is very effective when you need to work with specific data in a standard or custom report.
An example:
I like to filter on the pages that begin with /google-analytics. It’s easy to set this up:
I don’t have to go to the advanced filter section anymore.
If you know how to work with RegEx, you can literally set this up in seconds! You can apply regular expressions to the all pages report, but also to any defined content groups.
2. Setting Up Filters
In this article I have already shown a couple of filters that include regular expressions.
Make sure to use the RegEx in his module as it is the only way to build and apply all the filters that you need.
Note: use a test view first if you are unsure about your RegEx/filter combination.
3. Setting Up Goals
Google Analytics currently has four different goal types:
- Destination
- Duration
- Pages/Screen per session
- Event
In the category destination goals regular expressions really come in handy.
Very often the thank you page of a goal includes query parameters or an orderID and looks quite similar to other pages.
Setting up your Google Analytics goals with regular expressions is easy and effective!
Helpful articles:
- Your expert roadmap to defining actionable KPIs
- How to strategically set up goals in Google Analytics
- Macro and micro goals
4. Defining Funnel steps
In the screenshot above you can see that defining a funnel is optional. You can turn it on and set up a goal including 20 funnel steps.
I hope you don’t have to set up that many steps. Since your conversion rate will be pretty close to 0 then. ;-)
Anyway, the same as with your thank you page, regular expressions are really handy when setting up funnel steps in Google Analytics.
5. Setting Up Segments
On default, Google Analytics reports on All Sessions.
There are dozens of reasons why you would like to dig deeper.
For ad-hoc segmentation I recommend to use Segments.
It is much easier to set up your own segments if you master regular expressions.
Regular Expression Tester
Whether you are new to RegEx in Google Analytics or an advanced user, I always recommend to test your RegEx first.
There are two smart ways to test your regular expressions:
- Table filters: see whether the desired results are returned via this filter field
- Online tool (RegEx tester by ActualMetrics)
Another great tip from Tobias Kraeft:
- Regex101.com (fantastic online regex tester and debugger)
Regular Expression Cheat Sheet
Recently, I have tested all regular expressions to find out which ones are (still) valid in Google Analytics.
I have created a handy overview for you that includes all regular expressions that are currently available, including examples to accelerate your learning.
RegEx and Google Analytics API
It is not the right time to explain all details about the Google Analytics API.
There is one thing worth mentioning here.
It works differently, but you can use regular expressions in Google Analytics API queries:
You can also use regular expressions in filter expressions using the =~ and !~ operators. Their syntax is similar to Perl regular expressions.
Keep these two rules in mind:
- Maximum length of 128 characters
- Regular expression matching is case-insensitive
Well, this is everything I wanted to share.
I guess there is a lot to think about after reading this article.
Do you already use regular expressions in Google Analytics? What do you like or dislike about them? Happy to hear your opinion!
One last thing... Make sure to get my automated Google Analytics 4 Audit Tool. It contains 30 key health checks on the GA4 Setup.
André Mafei says
Great post, congrats Paul!
Just a small correction, change from:
77.120.120.1 to 77.120.120.99 …
to:
77.120.120.0 to 77.120.120.99 …
Paul Koks says
I am glad you like it and thanks a lot for your comment André! I have changed it.
Best,
Paul
Boaz Ariely says
Hi all,
What about if I need a regex for a funnel step, of all URLs that end with “.html” but do not contain “-c-” in them.
I understand that this syntax would not work:
.*(!?-c-).*\.html
Any advice on how to do this?
Paul Koks says
Hi Boaz,
Could you give me an example URL so I can look more closely into it?
Thank you,
Paul
Vitaliy Kolos says
Thanks for the article. Do I need to escape a hyphen in Google Analytics while adding a custom filter?
For example, will social-button\.xyz suffice or I need to actually escape the hyphen as well so that it looks as follows: social\-button\.xyz
Paul Koks says
Hi Vitaliy,
Thank you for your comment.
You don’t need to escape the hyphen, social-button\.xyz will do.
Two more things:
– You can always check your RegEx in the content reports to see whether it matches.
– You might want to add an include on hostname to rule out a lot of spam. I expect this traffic to be on (not set) hostname.
Thanks,
Paul
Charles Meaden says
This is very useful for recreating an AND statement in Regex
In this case it will only match a line that contains both green and red
green\\?.*red
Paul Koks says
Great use case! Yeah, RegEx is very powerful and every Google Analytics user should at least learn the basics.
Faiz says
hey, thanks so much for this post.
There is a typo, probably.
“The plus sign matches one or more of the precious characters.” — probably you meant ‘previous’ and not ‘precious’, n’est-ce-pas?
I have two URLs, one ending as x.html and another as x/y.html
If in a sales funnel, I want to match either of these URLs, can I use ^x(.*).
Paul Koks says
Hi Faiz,
Thank you for spotting the typo!
I would use (x|x/y)\.html$ instead.
– The problem with “^” is that it says “starts with”. That is not the case if you have other characters in front of “x”.
– You want to end the regex after “html”. That’s why I use “$”.
– The “.” is escaped in my regex.
I recommend to test this regex in the content reports (all pages) to make sure it works.
PS: you might want to check out this tool as well: http://www.analyticsmarket.com/freetools/regex-tester
Best,
Paul
Faiz says
hi
Thank you very much for a very detailed response. Really appreciate this!
Paul Koks says
Hi Faiz,
You’re welcome and I am happy I could help you out!
Paul
Ashley Riddle says
Hello Paul,
I’m a regex novice and I’m hoping you can give me some pointers. :) I have been trying to figure out a regex line for a GA goal. I need it to include all of my “thank you” pages, but not the thank you pages for our demo request. Here are a couple of URL examples:
Include: http://www.mydomain.com/survival-guide-TY-LP.html
Exclude: http://www.mydomain.com/demo-request-TY-LP.html
I need something that will look for TY-LP in the URL, but not if the URL also contains “demo”. Any ideas?
(I’m planning to redo our URL naming scheme so this is easier in the future, but for now I have to make do with the URLs that exist.)
Thanks in advance!
Ashley
Paul Koks says
Hi Ashley,
Is there something unique in the URLs that you want to “include”? That would make things a lot easier. In the goals setup Google Analytics forces you to use an “include” statement instead of combining “includes” and “excludes” like you can do in segments and filters.
So we need to find a way to group the ones you need.
Another way could be to include them all and use a segment to leave out the “demo” URLs while analyzing your data, but that is a bit cumbersome.
Thanks,
Paul
Timur says
Hello Paul, great post.
One note for readers, exclude (!) expression is not working in GA.
Paul Koks says
Hi Timur, good to hear you like the post. And you are right, that RegEx doesn’t work in GA.
David says
Hi Paul,
great article. I want to share the by far best regex checker and visualizer if found.
https://www.debuggex.com/ The visualisation helps me a lot to learn und and debug regular-expressions.
Paul Koks says
Thank you David! And I will surely check it out. Best, Paul
Wesley says
Ahhh — love the tip about testing statements in content reports. This allowed me to (1) avoid posting in Stack Overflow and (2) ID the correct regex statement /\.*Nice!
Paul Koks says
Great to hear Wesley. Yeah, it is really powerful, especially when setting up destination goals or content groups! But there are a ton of more reasons why you want to do it.
Matteo Zambon says
Very helpful article. Thank you, nice job!
Laura says
Paul, I need you. I am desesperated, I need add in my funnel goal this dynamic url
domain.com//register.do?t=be2f9761ac8af25152eff798792008188f776fc3999ea87fe49a07a9cf95e4d1&rd
How can I do it? I tried several uses, but it didn´t work.
Thank you
Paul Koks says
Hi Laura,
Please try this RegEx:
register\.do\?t=
Does that work?
PS: you can verify this in the content reports.
Paul
James says
Hi Paul,
This is fantastic! I am trying to setup a goal in Google Analytics to track anyone who starts on the below 4 urls (exactly those urls, including the query elements at the end) and finishes on the 5th. The 5th is the final goal, but they must start on those urls exactly.
I can’t work out how to do this as the ?takeOverError=true query at the end is crucial to track (as /order/.*/ part serves a compeltely different page). Exact match doesn’t carry query parameters across as far as I know?
How do you suggest I do this? REALLY keen to know.
Starts on:
/order/new-line?takeOverError=true
/order/stopped-line?takeOverError=true
/order/stopped-line?takeOverError=true
/order/unavailable/?setDefaultValues=true
Finishes on:
/order/.*/thanks
Paul Koks says
Hi James,
Thanks for your comment!
I think this should work:
^/order/(new-line\?takeOverError=true|stopped-line\?takeOverError=true|switched-line\?takeOverError=true|unavailable/\?setDefaultValues=true)$
You can verify it via the “All Pages” report. This to see whether it matches with the required URLs.
Cheers,
Paul
James (Martin) says
Thanks Paul – you are amazing and I’m going to start following you on Linkedin. Sorry, I should have been a bit clearer! I wanted to break each one out seperately in to different goals. How would each one look like? I’m hazarding a guess at ^/order/(new-line\?takeOverError=true)$ but I’m worried I’d get it wrong! Assuming I use regular expressions, too?
Goal 1:
Must start on this exact URL: /order/new-line?takeOverError=true
Finishes on: /order/.*/thanks
Goal 2;
Must hit this exact URL: /order/switcher-line?takeOverError=true
Finishes on: /order/.*/thanks
Goal 3:
Must hit this exact URL: /order/stopped-line?takeOverError=true
Finishes on: /order/.*/thanks
Goal 4:
Must hit this exact URL: /order/unavailable/?setDefaultValues=true
Finishes on: /order/.*/thanks
Paul Koks says
Hi James,
No problem! In that case, these should work:
1) ^/order/new-line\?takeOverError=true$
2) ^/order/switcher-line\?takeOverError=true$
3) ^/order/stopped-line\?takeOverError=true$
4) ^/order/unavailable/\?setDefaultValues=true$
You can leave () out as it is used to group items in a regular expression.
Happy to hear whether this works!
Cheers,
Paul
Vishnu says
Great post paul!
Wasn’t able to find one good article about regex for google analytics until i came across this one!
Paul Koks says
Hi Vishnu, I am glad to hear this article is useful to you! Best, Paul
Victoria says
Hi Paul,
Great article! I’m struggling to create a regex on the destination URL for my ecommerce Goal because there’s some variables on the URL. The destination page is something like:
/Payment/Thanks?Success=true&TransactionId=3057382365&descr=Miembro%20-%20Afiliado
where the TransactionIid is dynamic. I have been trying to replace the transaction number with the regex expression below, but I’m not sure if this is working properly.
\/Payment\/Thanks\?Success=true&TransactionId=\d+&descr=Miembro%20-%20Afiliado
Thanks :)
Paul Koks says
Hi Victoria,
Thanks for your comment! It seems that you can simply things a bit as the query parameters are always present.
You can try this regex: ^\/Payment\/Thanks\?Success=true
Please add it as a table filter to the All Pages / Content Reports. You can actually verify whether it matches with the desired URLs before setting it up as your goal URL.
Let me know whether it works!
Best,
Paul
Victoria says
Hey Paul,
Thanks for the quick reply! Your suggestion is working fine (thanks also for the tip for testing it!), however, I’ve got several destination pages that follow the same pattern, and what I’m struggling to get is the split between them:
/Payment/Thanks?Success=true&TransactionId=3057382365&descr=Miembro%20-%20Afiliado
and
/Payment/Thanks?Success=true&TransactionId=3057382365&descr=Miembro%20-%20Estudiante
and
/Payment/Thanks?Success=true&TransactionId=3057382365&descr=Miembro%20-%20Generico
Can I just add one keyword (e.g. Afiliado) to your regex suggestion to track each one of them? Something like:
^\/Payment\/Thanks\/Afiliado/
Cheers!
Paul Koks says
Hey Victoria,
In that case you could use this RegEx instead:
^\/Payment\/Thanks\?Success=true.*Afiliado
However, you can also find this information in the dimension “goal completion location”. So you would not have to set up separate goals for that.
Best,
Paul
Victoria says
Great! That seems to have solved my problem.
Thanks a lot Paul!
Paul Koks says
Great to hear Victoria and let me know if you run into any issues!
Kasper Kjær Eriksen says
Great post! I keep returning to it to improve my GA reports and setup.
Paul Koks says
Hi Kasper, great to hear this post is useful for you. Yeah, RegEx is extremely powerful and can be used in multiple ways.
Chris says
Hi Paul,
I am capturing downloads on my site as events. The event label is populated by the pathway to the file (e.g. http://www.mysite.com/my%20files/my%20document.pdf). I set a filter to replace %20 with a space, but it only replaces the first instance and leaves all remaining instances of %20. I have tried every regex combination I can think of to replace all instances without success. Any ideas?
Paul Koks says
Hi Chris,
I believe you are right with that: Google Analytics only replaces one instance per filter.
Do you know the maxium number of “%20” in your URLs?
A solution that might work is to create multiple filters of the same type: replacing “%20″ in ” “. I recommend to test this out in a test view to see whether it replaces all instances in that case.
Let me know how it goes!
Best,
Paul
Chris says
Hi Paul,
Your solution seems to be the only way. I created 15 identical filters and applied them to a test view with success. Hopefully, I won’t need any more than that.
Thank you for your help.
Paul Koks says
Great to hear Chris, I am glad it works!
Aaron says
So glad I found this. I have purchased several courses on Google Analytics and Adwords etc and setting up goals properly has always been a sticking point. I couldn’t find any info that explained RegEx in a way that I could understand it. I am not a programmer and my brain doesn’t work that way but I wish it did. This was super super helpful. Look forward to checking out your other post and offers. Thanks again for the content, so helpful!
Paul Koks says
Thanks for your comment and I am glad this post is useful! I recommend to check out the “start here” section to learn more about very important digital analytics / GA topics.
Cindy Tribucher says
Is it possible to track URLs with anchor links in them via a regular expression/goal?
We would like to be able to see which anchor links are clicked on our Current Openings page; please see http://gardner-webb.edu/offices-and-departments/departments/human-resources/current-openings/index
Each position has a unique anchor link that and our HR team would like to know which ones are clicked. For example: http://gardner-webb.edu/offices-and-departments/departments/human-resources/current-openings/index#faculty-british-literature
Appreciate any insight you may have!
Paul Koks says
Good question Cindy! In Google Analytics, fragment changes are not tracked by default, and the URL paths that are passed to GA with your Pageview hits are stripped of these fragments. With Google Tag Manager, this can be remedied with a History Change Trigger and some Variable magic.
Read more here: https://www.simoahava.com/gtm-tips/track-url-fragments-as-pageviews/. You probably need some dev help to get this done.
After, you can find out which links are clicked on in GA. You don’t have to use regex or goals to read the stats.
You can type in “/offices-and-departments/departments/human-resources/current-openings/index” in the table filter box and review the results.
Cindy Tribucher says
Hi Paul,
I have been behind on thanking you for your input! I have followed the instructions on Simo Ahava’s blog; that was very helpful. From what I can see, I have set up the tags/triggers correctly; I’m just waiting for a colleague to publish the newest version of GTM for me.
I appreciate your time – thanks again!
Cindy
Paul Koks says
Sounds great Cindy, hope it works all properly! Best, Paul
Ferhat says
Hi Paul,
I want to ask a question. I try to make number range but i cant do.
it is number–> 10000-1000000
Thanks.
Paul Koks says
Hi Ferhat,
This one should do the job I think:
^[1-9][0-9]{4,5}$|^1000000$
Let me know if it works for you!
Best,
Paul
INBARAJ AUGUSTIN says
Hi Paul,
I have two url
1./order/ORD01186z7xzz86
2./login?target=/order/ORD011864ab54x6
I need to exclude this /login?target=/order/ORD011864ab54x6 in goal. so, I have created Regular expression rule (^/order/ord[0-1]) in funnel.
Can you please tell me that Rex rule is correct or not.
Thanks,
Inbaraj Augustin
Paul Koks says
Hi Inbaraj,
Your RegEx will definitely exclude the second (login) URL as you state it should start with “/order”.
Does the order ID follow a particular pattern? If this is not the case, you could use “^/order/ord” – remove two times “. This URL will automatically pick up all order IDs.
One last thing, use the content/all pages report to quickly verify your RegEx.
Best,
Paul
INBARAJ AUGUSTIN says
Thanks Paul,
This one ^/order/ord is working and /order showing both the urls.
Kritika says
Hi Paul,
If I have a string ” a for apple ” and I want to replace only first ‘a’ not the other ones in the given string. which regex should I use to replace with other string? can you help me out?
Regards,
Kritika
Paul Koks says
Hi Kritika,
I am not sure whether I understand your query correctly.
Here are the steps based on your example / question:
– Filter type = Custom
– Search and Replace
– Select the “dimension” in the Filter Field
– Search String equals “a for apple”
– Replace String equals [the entire string you want to see instead of “a for apple”]
And always use a Test View to test your filter.
Best,
Paul
Ostii says
So good. Thanks Paul.
This has become my GoTo guide for Regex in Google Analytics.
Paul Koks says
Thanks for your comment and great to hear!
Tim Chard says
Hey Paul! Thanks for this guide. Question for you:
^/free/(thanks)/$
Would this be saying “complete the goal for all pages that contain /free AND contain “thanks” in the URL string thereafter”? Issue is client has multiple different lead thank you pages leads that follow /free, but unfortunately has ‘thanks’ in all of them in different places (“lead-thanks” and “thanks-lead”, etc.).
Any idea? I know you’re busy, but I thought I’d give it a shot!
Paul Koks says
Hi Tim,
This RegEx won’t work properly. It would only match “/free/thanks/”.
You can try out this expression in your content reports (All Pages):
^/free.*thanks
Happy to hear if it works!
Thanks,
Paul
Leonie says
Hi Paul,
Thanks for the great guide. I want to exclude IP addresses.
How would you transform these to a regex?
142.16.0.0/12
196.186.0.0/16
thanks!
Paul Koks says
Great question Leonie!
In some cases you may encounter an IPv4 address with a subnet range. Then you need to use a workaround to generate the IP address ranges.
You can use this tool for that:
https://mxtoolbox.com/subnetcalculator.aspx
First one
https://mxtoolbox.com/subnet/?filter=142.16.0.0/12&source=subnettool
^142\.(1[6-9]|2[0-9]|3[0-1])\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))$
Second one
https://mxtoolbox.com/subnet/?filter=196.186.0.0/16&source=subnettool
^196\.186\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))$
Let me know if this works for you!
Best,
Paul
Jack says
I have a question that I’m struggling to find a solution for online. Bear in mind, I’m a rookie with GA and regex.
I want to set up a goal in GA where a user gets to the thankyou page which looks like this:
http://www.domain.com/thank-you-for-leaving-a-review/5985
The last part is a unique user ID and I don’t want to set up a goal for each individual user. Instead, I want to set up a goal for when anyone gets to this thank you page. How do I exclude the 5985 from the goal destination URL?
I have tried starts with; http://www.domain.com/thank-you-for-leaving-a-review/ but this doesn’t seem to work.
If this should work, potentially there is a problem with the tracking code. Any help would be greatly appreciated!
Paul Koks says
Hi Jack,
In this case you can choose “Begins With” or “Regular expression”.
– Begins with: /thank-you-for-leaving-a-review/
– Regular expression: /thank-you-for-leaving-a-review/.*
The trick is not to add the domain here as it doesn’t appear in your “All Pages report”.
Best,
Paul
Ado says
Hi Paul,
I am having trouble excluding some added parameters from two destination goals. What would be the best way to set up the below as destination goals in Analytics?
1. /thank-you-survey?submissionGuid=357efc5c-357f-47cc-8e19-4b81ddb724d3
2. /thank-you?submissionGuid=081fc023-70cf-4012-81d6-9cae43f2c5ff.
Everything after the = is randomly generated.
Paul Koks says
Hi Ado,
I am used to setting these up with a RegEx match and this should work:
1. ^/thank-you-survey\?submissionGuid=
2. ^/thank-you\?submissionGuid=
Best,
Paul
Jaideep says
Hi Paul,
Can I use OR(|) expression in Event Category or any event goal
Paul Koks says
Hi Jaideep,
I think I have never tried, but this should work as they offer “Regular expression” as one of the matching options.
Andrew Wong says
Thanks for the useful regular expression information.
I’ve a very complicate regular expression to return me URLs with lot of parameters and values.
How to write the regular expression to exclude specific words that appear at the middle of the URL ? such as I want to filter out “departure” from the following URLs :
/tc/travel-insurance?promo=CityUFWD
/tc/travel-insurance?promo=TRA123&promo=TRA123
/tc/travel-insurance?departureDate=23-12-2018&returnDate=26-12-2018&plan=personal&traveler=1
/tc/travel-insurance?promo=GOB123&departureDate=14-03-2019&returnDate=19-03-2019&plan=family&traveler=3&adult=2&child=1&other=0
Paul Koks says
Could you exactly explain what you are trying to accomplish? Do you want to rewrite those URLs or something else?
For replacing:
Use a “Search and Replace filter” on departureDate and replace it by Date (on dimension Request URI).
For completely removing the parameter departureDate:
Use a query parameter filter at the view level.
You probably don’t need a RegEx here.
But once again, it is not completely clear to me what you are exactly trying to accomplish.
Hope this helps!
Ryan Fernandez says
One thing to mention for all is that using RegEx Segment filters for let’s say hostname, you’ll need to remember that these are only scoped at a User or Session level. A prime example is when using your ALL view that may have multiple hostnames under one property, you can apply the ‘Matches RegEx’ ^www.domain.com and still get other domains coming through because a session that includes http://www.domain.com can still technically include subdomain.domain.com.
Just something to be mindful of – thanks for putting this together, Paul!
Paul Koks says
Thanks for the heads up Ryan! Good point, using segments filters can be really powerful, but you need to know what you are doing!
grant says
Might be a little late on this but wondering if you could help me out.
I am trying to write a regular expression for a couple different goal locations to all be counted towards one goal.
The URL’s I am trying capture are listed below
– /get-approved/get-approved-complete or /get-approved/get-approved-scr-complete
– /get-approved/get-approved-cmp-complete?cmp=ppc(any number)
– /dealerships/buy-(city)-(area)-(storeID)/inventory?cmp=ppc(any number)
– /dealerships/buy-(city)-(area)-(storeID)/inventory?cmp=T(any number)P(any number)
But in some cases the URL’s that start with /dealership will end with &pagenumber
– /dealerships/buy-(city)-(area)-(storeID)/inventory?cmp=ppc(any number)&pagenumber=1
– /dealerships/buy-(city)-(area)-(storeID)/inventory?cmp=T(any number)P(any number)&pagenumber=2
We do not want to include these pages in our overall goal completion location. But am not sure if I am using all the functions properly. Would you mind taking a look?
I am currently using this as my regex string
complete|cmp=ppc.$|cmp=T.P.$
| = OR
$ = End of String
. = any character
Thanks,
Grant
Paul Koks says
Hi Grant,
This one looks really complicated and is very difficult to test out on my end.
It looks good to me, but you should test it in the All Pages report – at least that is what I recommend to find out exactly with what URLs/data it matches.
One thing: you can replace “.” with “[0-9]” to indicate “any number”.
Best,
Paul
jeet says
Hello Paul,
I wanted to know while setting how can I use matches regex for the visitors at a specific page of my site. /secure/viewinvoice.php?id=ABCD, I wanted to track all the users exiting this page
Paul Koks says
Hi Jeet,
You can use this regex to match the page:
^\/secure\/viewinvoice\.php\?id=ABCD$
Hope this helps!
Best,
Paul
chs rougeux says
Hi. I keep coming back to your article. it’s great! If I wanted to make a regular expression of branded keyword search terms to include or exclude in a segment, what is the best way to do that? They can appear anywhere in the search of course. as the first word, a midde word, or the last word.
Am I over-thinking it? Hard to know right now.
my results of the regex output should include the following scenarios:
word word word BRANDED-WORD word word word
word word word BRANDED-WORD word word word
word word word BRANDED-WORD
BRANDED-WORD word word word
BRANDED-WORD
and
word word word BRANDED-WORD2 word word word
word word word BRANDED-WORD2 word word word
word word word BRANDED-WORD2
BRANDED-WORD2 word word word
BRANDED-WORD2
Paul Koks says
Hi there,
Please see below how you can tackle it.
Example where BRANDED-WORD1 = BW1 and BRANDED-WORD2 = BW2.
Use this RegEx:
BW1|BW2 and in case of three words BW1|BW2|BW3
Hope this helps!
Best,
Paul
chs rougeux says
I knew I was over thinking it. Thanks.
Paul Koks says
Glad it helps!
Lee says
Hi Paul,
Do you know of a simple way to exclude strings from a hostname or URL? Let’s say I want to exclude all subdomains for test.com except for the subdomains direct.test.com and jobs.test.com, and keep other hostnames like test1.com and test2.com for which there may be too many to include manually.
Thanks,
Lee
Paul Koks says
Hi Lee,
You will probably have a challenge as you can only work with an “include” or “exclude” filter on hostname.
So if you make use of an include, you would need to catch all hostnames in addition to: ^(direct|jobs)\.test\.com$.
It is very hard for me to help as I would need to have more insights on how the domain names etc. are exactly structured.
Hope this gives some help!
Best,
Paul
Martin says
Hi Paul,
I want to set a goal on GA which should include all pages containing a specific word (platform) on just one domain. Even though I read the article through, I still have no idea how to do that. Could you please help me out?
Best,
Martin
Martin says
Quick edit, all URLs are simple so the structure across the whole website is as follows (with the single letters replacing random words):
http://www.domain.com/x-plattform-z or http://www.domain.com/plattform-y-c or http://www.domain.com/d-s-plattform
I am not sure if I am going in the right direction but something like (.*)plattform(.*) should work just fine – correct me if I am wrong.
Best
Martin
Paul Koks says
Hi Martin,
Yes, I think this works. Please test it in the “All Pages report” to double check.
Best,
Paul
Mahendra says
Hi Paul,
Great content.
The regex created works in All pages in Site Content, but not in Goals for destination. What could be the reason.
Paul Koks says
Hi Mahendra,
Not sure what is causing this as I haven’t encountered it myself. Normally it always works (on my end).
I can only recommend double checking your settings and posting your question elsewhere to see whether someone encountered the same issue.
Best,
Paul
AS says
Hi ,
I am trying to create Goal on Google analytics using Regex , want to create for Thank you page for local lang site ,below is the format i added …
My thank you url looks like this /da-dk/thank-you?submissionGuid=
Regex used –> \/da-dk\/thank-you?submissionGuid
But it didnot work for me , Could you please help me to create regex for the lang url.
AS says
Hi ,
I am trying to create Goal for lang page url that looks like this
/da-dk/thank-you?submissionGuid=
Regex used \/da-dk\/thank-you?submissionGuid
But it didnot work on Google analytics ..
Please correct me if it is wrongly done.
Paul Koks says
Hi there,
You need to add one thing (escape the “question mark”):
\/da-dk\/thank-you\?submissionGuid
This should work!
Best,
Paul
AS says
It didnot work by removing ? mark ,
Paul Koks says
You shouldn’t remove the ? mark, but you need to escape it (put a backslash before it):
\/da-dk\/thank-you\?submissionGuid
This is because a ? mark is a RegEx character and you need to treat it as a normal character to make it work (that’s why you need to escape it).
AS says
Hi Paul,
I tried doing same way as you mentioned but its not working on Google analytics (unable to verify) , i would like to share the screenshot but i dont see that option ..
Regex used now : \/da-dk\/thank-you\?submissionGuid
AS says
Thanks Paul its working, its taking some time to reflect..
Thank you so much..
Paul Koks says
Glad to hear that it works. One last tip: you can try out the RegEx within the “All Pages” section to see if it works (based on historical data of visited pages).
AS says
Noted Paul :-)
Thanks again !
Paul Koks says
Happy to help :-)
Simon Pointer says
Hi Paul. Great post – thanks. I have been using it for ages as the best GA specific resource for Regex when I train GA training courses. I wanted to request an addition perhaps?:
I often do a regex example using geo data to return a table of multiple cities, something like – London|Glasgow|New York|Paris and it works a treat bar one key issue:
The match pattern is effectively: contains London OR contains Glasgow OR contains New York etc etc. The problem is for cities like London I will also get Londonderry, New London, East London etc and this kind of defeats the object of trying to Isolate these specific cities in the first place.
I have looked for a solution for a little while for exactly London OR exactly Paris OR exactly Other city, but of course you can’t just do “London” | “Paris” or even (London) or ‘London’ or \London\ or [London] or {London} because these regex characters don’t do exact match on a string and doing that seems quite hard.
Today I found a nice solution on one of google’s own support pages for work space admins and it works a treat for this so thought I’d share it here as it might help out other people too.
(\W|^)London(\W|$)|Glasgow|New York|Paris|Milan
Paul Koks says
Hi Simon,
Great to hear from you and thank you for the heads up and suggestion!
I think this one will also work in your case (to go for “exact” match):
^(London|Glasgow|New York|Paris)$
By using “()” I group all cities together, “^” defines the starting point and “$” defines the end point.
Best,
Paul
Pieter says
Hi Paul, I’m trying to set up a Goal in Google Analytics using Regular expression. It’s part of an application funnel that contains 4 steps where the end of the URL equals /apply?&step=1. Where 1 is the first step and 4 the confirmation page. I’ve been trying to make it work but without succes (yet). How should I write this? Can anyone point me to the right direction?
Paul Koks says
Hi Pieter,
This should work for you:
\/apply\?&step=1
Best,
Paul
mohi says
I need to track my news in one specific category. what I have problem with, is that the post on that category doesn’t have regex. this is my main category :domain.com/category/market/news. this is one example:
https://tejaratnews.com/category/%d8%a8%d8%a7%d8%b2%d8%a7%d8%b1/%d8%a8%d9%88%d8%b1%d8%b3
but this is my posts url sample: domain.com/poet-title
How can track traffic volume of all my posts of that specific category?
Paul Koks says
Hi Mohi,
In that case you would probably need the help of a developer to write a code/rule that all news posts are automatically tracked in a “news” content group.
This is very challenging to achieve with a simple RegEx rule as you already mention.
Best,
Paul