Maybe you’ve heard of regular expressions, but aren’t quite sure how to use it in SEO, or if it fits your own strategy.
Regular expressions or “regular expressions” are similar to built-in programming languages for text searches, allowing you to include complex search strings, partial matches and wildcards, case-insensitive searches, and other advanced directives.
You can think of them as search patterns rather than specific text strings.
As such, they can help you find a whole set of search results that, at first glance, appear to have little in common with each other.
Regular expressions are a self-contained language that may seem foreign when you first see it.
But they are easy to learn and work across platforms JavaScript, Python and other programming languages, making them versatile and powerful SEO tools.
In this guide, you’ll learn about common regular expression operators, how to use more advanced regular expression filters for SEO, how to use regular expressions in Google Analytics and Google Search Console, and more.
You will also find examples of regular expressions in SEO in different ways.
What does a regular expression look like?
Regular expressions typically include combinations of text that will be matched exactly in search results, as well as some operators that are more like wildcards for pattern matching rather than exact text matching.
This can include single-character wildcards, matches of one or more characters, matches of zero or more characters, as well as optional characters, nested subexpressions in parentheses, and the OR function.
By combining these different operations, you can build a complex expression that can achieve very far-reaching but very specific results.
Common Regular Expression Operators
Some common examples of regular expression operators include:
. Wildcard match for any single character.
.* matches zero or more characters.
.+ matches one or more characters.
d matches any single digit 0-9.
? is inserted after the character to make it an optional part of the expression.
| The pipe or “pipe” character represents the “or” function.
^ is used to indicate the beginning of a string.
$ is used to indicate the end of the string.
( ) is used to nest subexpressions.
Insert before an operator or special character to “escape” it.
Some programming languages, such as JavaScript, allow “flags” to be included after the regular expression pattern itself, and these further affect the result:
g returns all matches, not just the first one.
i returns a case-insensitive result.
m activates multiline mode.
s Activate “dotall” mode.
u Activate full Unicode support.
y Search for a specific text position (“sticky” mode).
As you can see, these operators and flags together start building complex logical languages that enable you to achieve very specific results across large unordered datasets.
How do you use regular expressions for SEO?
Regular expressions can be used to explore the queries used by different segments of users, which queries are common to specific content areas, which queries drive traffic to specific parts of your site, and more.
exist This articleFor example, Hamlet Batista demonstrates how to use regular expressions in Python to analyze server log files.
while in this, Chris Long shows you how to use regular expressions to extract the location, item, and name of the breadcrumbs associated with each URL of your site as part of a scalable keyword research and segmentation process.
Google encourages SEO pros share examples of how they use regular expressions on Twitter using hashtags #performance regex.
Here are some tips from SEO Twitter (you’ll notice this is a pretty quiet hashtag – add your own example if you have one!):
Use slug$ in the filter to see a list of every page/keyword ending in “slug”. This is very important if you have to manage a large website 🖤#performance regex
– hannes-jeremia jaacks (@HannesJaacks) December 31, 2021
I have compiled a fairly extensive library #Common expressions for #googlesearchconsole. 😎
Hit me up if you have any other ideas, happy to add them. @danielwaisberg @DanielHereMe @CyrusShepard @5le @DataChaz #performance regex #regular expression #seo https://t.co/BKX9UCGrOU
— JC Chouinard (@ChouinardJC) June 17, 2021
Using Regular Expressions with Google Analytics
One of the most common uses of regular expressions for SEO is Google Analytics, where regular expressions can be used to set up filters so that you only see the data you want to see.
In this sense, expressions are used to exclude results, rather than producing an inclusive set of search results.
For example, if you wanted to exclude data from IP addresses on the LAN, you could filter out 192.168.*.* to remove the entire range from 192.168.0.0 to 192.168.255.255.
More advanced regular expression SEO filters
As a more complex example, suppose you have two brands: regex247 and regex365.
You may want to filter results that match any combination of URLs that contain these brand names, such as regex247.biz or www.regex365.org.
One way is to use a fairly simple “or” expression:
.*regex247.*|.*regex365.*
This will remove all matching URLs from your Analytics data, including subfolder paths and specific page URLs that appear on those domains.
a warning
It’s worth noting that – similar to your robots.txt file – a poorly written regular expression can easily filter out most or all of the data by including an unrestricted wildcard match.
The good news is that in many SEO cases, filters are only applied to your data during the reporting phase, and by editing or removing your regular expressions, you can restore full visibility into your data.
You can also test regular expressions on a number of online testing tools to see if they perform as expected – allowing you to “sandbox” regular expressions before releasing them to the entire dataset.
To create a regular expression filter on Google Analytics, first, navigate to the type of report you want to create (e.g. behavior > website content > all pages or get > All traffic > source/Moderate).
At the top of the data table below the graph, find the search box and click advanced Show advanced filter options.
Here you can include or exclude data based on specific dimensions or metrics.In the drop-down list after selecting a dimension, select match regular expression Then enter your expression in the text box.
“or” and “and” in google analytics regex
To create an “or” expression in Google Analytics, simply add a pipe character (| vertical pen symbol) between the corresponding segments of the expression.
Google Analytics regular expressions do not support “and” statements within a single regular expression; however, you can add another filter to achieve this.
Below your first regex, just click Add a dimension or metric and enter your next regular expression. In this way, you can stack as many expressions as you want and process them as a single logical AND statement when filtering data.
Using Regular Expressions in Google Search Console
In 2021, Google Search Console will begin supporting the Re2 syntax for regular expressions, allowing webmasters to include and exclude data in the UI.
You’ll find all metacharacters supported by Google Search Console in This RE2 regular expression syntax reference on GitHub.
As of this writing, the character limit is 4096 characters (usually enough…).
An example you can use in Search Console can filter queries that contain specific brands and variations that users can type, such as Facebook:
.*facebook.*|face*book.*|fb.*|fbook.*|f*book.*
Filter out users who find your site through a “commercial” intent clause:
.*(best|top|alternative|alternative|vs|comparison|comment*).*
related: Google Search Console adds new regex filter option
Why are regular expressions important for SEO?
In the end, why does this all matter?
Well, it’s all about taking control of your data and filtering out the parts that don’t help you improve your SEO – whether it’s specific pages or parts of your site, traffic from specific sources or media, or your own local network data.
You can create very simple regular expressions to implement basic “include” or “exclude” filters, or write longer expressions similar to programming code to achieve complex and very specific results.
And with the correct regular expressions for each campaign, you can verify that your SEO efforts are achieving your goals, ambitions and results – an efficient way Demonstrate positive ROI About your future SEO investment.
More resources:
Featured Image: Optura Design/Shutterstock



