A regular expression (regex) is a short pattern that matches many text variations at once. In SEO, it lets you replace a stack of filters with one.
When I first started out in SEO, I had never heard of a Regular Expression. I’ve since learned that, while intimidating at first, RegEx are an invaluable tool and a little understanding of how they work goes a long way.
This guide shows how regex works and how to use it in day-to-day SEO work, starting with a real filtering problem.
The Challenge: Filtering Out Misspelled Brand Queries
I was working with a company called Bryers’ Widgets (names have been changed to protect the innocent). As part of my keyword research, I wanted to see what non-branded (discovery) keywords were generating impressions and clicks for the site. Using Google Analytics I viewed the list of queries that generated impressions in the past 12 months.
This was a good start, but the list showed ALL keywords, both branded and non-branded. I needed to filter out any queries that contained the client’s name “Bryers’ Widgets”. Easy enough, right? I clicked on the advanced filter and chose “Exclude Query Containing Bryers’” as seen here:
However, when I reviewed the results, a problem became very evident. People were misspelling the company name in at least 10 different ways:
| Briers’ | Breyers’ |
| Briar’s | Bryers |
| Buyers | Bryars’ |
| Breyars | Breyar’s |
| Breir’s | Bryer |
Now, I could have kept adding filters for each misspelling (after all, I needed to exclude all branded terms whether spelled correctly or not), but in the end I would have needed a minimum of 10 separate filters. That is a laborious task at best.
If you report on branded and non-branded traffic, you will run into this often. Our guide to how to separate branded and non-branded queries in GA4 covers the reporting side. The same filtering approach works in current tools. Search Console supports regex filters for page URLs and queries, and GA4 filters can match on a regular expression.
Can One Regular Expression Replace 10 Filters?
Yes. One regular expression can match every spelling variant, so a single filter does the work of ten.
What Is a Regular Expression (RegEx)?
A regular expression (regex) is a short pattern that matches many text variations at once. It lets you filter a whole family of similar queries with one rule. Regex uses characters and meta characters to do this.
A character is any letter, number, or symbol (or group of letters, numbers, or symbols) that you would like to match. In this case, I want to match all of the different ways in which people have spelled “Bryers’.”
A meta character is a special set of characters that you will use to define what you’re looking for. There are a ton of meta characters that you can use. A great quick reference of meta characters used in RegEx can be found here: https://www.regular-expressions.info/refcharacters.html.
For this example, we will use only 3 of the most basic meta characters to accomplish our goal:
Meta character | What it does | Example |
( ) | Groups characters so they act as one unit | (Bryers) treats Bryers as one group |
| | Means “or” | (B|b)ryers’ matches Bryers’ or bryers’ |
? | The previous character occurs 1 or 0 times | Bryers? matches Bryers and Bryer |
Note:One key meta character that we will not use but is important to know is the \. You may have asked yourself, “What if I need to match a question mark? Because it’s a meta character, won’t it simply modify the previous character?” The answer is yes. BUT a \ exempts the following character from being meta. So \? would be seen simply as a question mark and not as a meta character.
How Do You Build a Simple RegEx, Step by Step?
Build the pattern one letter position at a time, using a group and an “or” wherever the spellings differ, and a question mark wherever a letter is sometimes missing.
Looking back at our list of keywords, we can take this piece by piece:
| Briers’ | Breyers’ |
| Briar’s | Bryers |
| Buyers | Bryars’ |
| Breyars | Breyar’s |
| Breir’s | Bryer |
Luckily every word starts off with a b! But, as mentioned in our examples, that b may or may not be capitalized. So again, we’ll use the parentheses to group them together and add the pipe meta character to indicate that either one OR the other is acceptable:
(B|b)
The third letter is a similar situation. But in this case, some people use the r, others have added a u, and still others have used neither and moved straight to the y. So we need to account for all 3 cases. Once again, we’ll use the parentheses and pipe combo to capture the r and the u, but we’ll add the question mark to indicate that this character may or may not occur at all:
(B|b)(r|u)?
Next, I see there is either an i or an e but sometimes neither of these:
(B|b)(r|u)?(i|e)?
Then some people have added a y, but not all of them, so we’ll use the question mark again:
(B|b)(r|u)?(i|e)?y?
Then we have either an e or an a:
(B|b)(r|u)?(i|e)?y?(e|a)
Then a bloody miracle occurs! Everyone remembers the r:
(B|b)(r|u)?(i|e)?y?(e|a)r
Then we have to deal with the apostrophe. Everyone has used an s at the end, but some put the apostrophe before the s. Some put it after. Some don’t use it at all. Again, we’ll go back to the trusty question mark to indicate that the previous character may occur 0 or 1 times:
(B|b)(r|u)?(i|e)?(i|y)?(e|a)?r’?s?’?
Two small fixes make this version match all 10 spellings: an optional i or y after the second letter group, so Breir’s works, and an optional s, so Bryer works.
And there you have it! We have built a simple RegEx that will capture all 10 versions of the keyword with one simple line. Now I can go back into my analytics tool, and instead of creating 10 filters each using “Exclude Query Containing”, I can create one simple filter using “Exclude Query Matching RegExp” as seen here:
The above filter will leave us with nothing but non-branded queries for our keyword research.
Check the results before you rely on them. A short pattern with optional letters can also match unrelated words (this one matches “bar,” for example), so scan the remaining query list for surprises. Case also matters: GA4 regex is case sensitive by default, which is why the pattern includes (B|b).
Where Can You Use RegEx in SEO Today?
You can use regex in Search Console and GA4, two places where most search marketing reporting starts.
• Search Console performance report: regex filters apply to page URLs and queries, and use RE2 syntax (Search Engine Land).
• GA4 filters: regex is full match and case sensitive by default, so add .* to a pattern when you want a partial match (Google Analytics Help).
• Redirects and canonicals: regex can match groups of URLs with one rule.
New to GA4 reporting? Start with our guide to navigating GA4.
A Final Word on RegEx
Hopefully you now have a basic understanding of what a RegEx is, how it works, and an example of how it might be used. As I mentioned in the beginning, Regular Expressions can be far more complex than this example, and there may even be simpler ways to accomplish the above using more complex language. That’s what makes RegEx useful! With some study, you’ll be able to build a RegEx to match a wide range of query and URL patterns. This tool also comes in handy when doing redirects, canonicals, and much more.
You don’t have to memorize any of this. Test each pattern on your own data and build up from there. If you would rather hand off the filtering and reporting setup, our Organic Search team can take it on.
More Resources:
https://moz.com/blog/an-seos-guide-to-regex
https://regexone.com/lesson/introduction_abcs
Frequently Asked Questions
What is regex in SEO?
Regex, short for regular expression, is a pattern that matches text. In SEO, you use it to filter or group queries and URLs, such as separating branded from non-branded searches.
Does Search Console support regex?
Yes. Search Console performance report filters support regular expressions for page URLs and queries, and use RE2 syntax.
Does GA4 support regex?
Yes. GA4 supports regular expressions in filters. By default the match is full and case sensitive, so add .* when you need a partial match.
Why does my regex match things I didn’t expect?
Optional characters make a pattern broad. Test it against your real query list, then tighten it by removing optional characters you don’t need.
Do I need to memorize regex syntax?
No. Keep a reference open, such as the one linked above, and build patterns one piece at a time like the example above.
This content was originally published in 2015 and was updated by our team in 2019. It was substantively updated by Leah Hart in October 2026.



