regex for seo
13 / 08 / 2025

Regular Expressions (REGEX) for SEO: what they are and what they’re used for

Carles Fillol: SEO and Digital Marketing specialist
Carles Fillol
SEO Technician

Discover how to use regular expressions (regex) in SEO for better filtering, automation and analysis across Google Analytics, Search Console, Sheets and .htaccess. Master technical SEO with precision.

In the world of technical SEO, every detail matters. Data is what helps us make strategic decisions. But how do we filter, interpret, and leverage large volumes of data efficiently? With regular expressions, also known as regex.

At first glance, they may seem complex or too technical, but if we understand them, regular expressions are an extremely useful tool for those working in web positioning. They allow us to identify patterns in texts, URLs or queries, filter information in tools like Google Analytics or Search Console, and even automate repetitive tasks in spreadsheets.

What is regex?

Regular expressions, commonly known as regex, are sequences of characters that define a search pattern. Although their origin is in the world of programming, their usefulness has spread to multiple areas, including SEO.

Simply put, regex allows you to find, filter, or modify text strings that follow a specific pattern. For example, you could find all URLs ending in ".jpg" or all titles that contain a specific word. This ability to filter data so precisely makes them a very powerful tool.

Main expressions and basic symbols

To start using regex in SEO, it’s essential to know the most common symbols:

SymbolFunction
.Represents any character
^Indicates the start of a string
$Indicates the end of a string
*Matches the previous character 0 or more times
+Matches the previous character 1 or more times
?Makes the previous character optional
()`Groups expressions
[]Defines a character class
\Escapes special characters

Simple examples of use:

  • /blog/.* → Matches all URLs starting with /blog/
  • ^https:// → Matches strings that start with https://

Where to use Regex

All this is great, but let’s give it a practical use so we can apply all these regular expressions and make our work easier. Below I’ll tell you the different tools where you can use them.

Regex in Google Analytics (GA)

Google Analytics 4 allows the use of regular expressions in various sections to segment, filter, or set conditions. Some key uses include:

Custom filters in reports

You can apply regex in filters to show only certain events, page views, or conversion paths.

Example:

^/blog/.*  → Includes only URLs starting with /blog/

Segment creation

When you create custom segments (for example, sessions that contain a specific product category), you can use regex to include/exclude behaviors.

Event configuration

GA4 allows configuring events based on matches with regular expressions. For example, to measure clicks on external links containing "oferta":

.*oferta.*

Exclude internal traffic (IPs, URLs, UAs)

You can use regex to exclude internal traffic (such as from your team) based on IP patterns or parameters.

Regex in Google Search Console (GSC)

Since 2021, GSC allows the use of regular expressions in "queries" and "pages" filters. This offers a lot of flexibility in analysis.

Query filter

You can find search patterns more precisely.

^comprar.*  → Keywords that start with "comprar"
.*gratis$   → Keywords that end with "gratis"

Page (URLs) filter

Filter URLs by subdirectories, parameters, or patterns.

^/categoria/.*/producto  → URLs that contain category/product-like structures

Use cases:

  • Analyze the CTR of a product family
  • Filter transactional queries
    .*precio.* → Matches any text containing the word "precio"
  • View performance of URLs with parameters
    \?ref= → Matches URLs that contain the parameter "?ref="

Regex in Screaming Frog

Screaming Frog is a powerful tool for auditing websites, and regex expands its functionality even further.

Custom filters

You can create filters in the crawler based on URLs, titles, descriptions, H1s, etc.

.*\.pdf$ → Filter all URLs ending in .pdf

Custom extraction

Extract specific HTML data such as prices, headers or metadata using regex:

Price: (\d+,\d{2})€

Report segmentation

Use it in "Config > Include / Exclude" to crawl only specific sections.

Use in Sitemaps and robots.txt

Analyze which URLs are being blocked or included according to patterns.

Regex in Google Sheets

Google Sheets allows the use of regex with very useful functions for automating and cleaning SEO data:

REGEXMATCH()

Checks whether a text matches a pattern.

=REGEXMATCH(A2, ".*producto.*")

REGEXEXTRACT()

  • Extracts a specific pattern.
=REGEXEXTRACT(A2, "/producto/([a-zA-Z0-9\-]+)")

REGEXREPLACE()

  • Replaces part of a string with another.
=REGEXREPLACE(A2, "(\?.*)", "") → Removes URL parameters

Useful cases:

  • Separate UTM parameters
  • Detect errors in URLs
  • Extract slugs, category names, IDs

Regex in .htaccess

In Apache, .htaccess files allow for conditional redirections using regular expressions.

Pattern-based redirection

RewriteRule ^blog/([0-9]{4})/(.*)$ /archivo/$1/$2 [R=301,L]

Redirects URLs like /blog/2022/post-title to /archivo/2022/post-title

Blocking IPs or bots

RewriteCond %{HTTP_USER_AGENT} ^.*(AhrefsBot|SemrushBot).*$ [NC]
RewriteRule .* - [F,L]

Parameter control

You can detect parameters and redirect, rewrite or block based on them.

Regex with ChatGPT

You can use ChatGPT to:

  • Write expressions from scratch
  • Fix broken expressions
  • Turn needs into patterns (natural language → regex)
  • Generate filters for specific tools (GSC, Sheets, etc.)

Example of a useful prompt:

"I need a regular expression to detect URLs with /categoria/ followed by a number and ending in /"

ChatGPT would reply with something like:

^/categoria/\d+/$

Validators and useful resources

There are several sites that help us validate whether we're using regular expressions correctly.

  • Regex101: Ideal for testing, debugging and understanding regular expressions.
  • Google Help – Regex in GA4

Regular expressions are an extremely powerful tool for those working in technical SEO. Although they may seem complex at first, learning them opens up a world of possibilities for optimizing, analyzing, and making more precise decisions.

And as you can see, they have multiple uses — you can apply them in Search Console, Analytics, spreadsheets or even .htaccess. Understanding regex can make a big difference in your SEO strategies.

Carles Fillol: SEO and Digital Marketing specialist
About the author
Carles Fillol — SEO Technician
My passion for SEO began with small niche projects, but over time I've broadened my horizons. I've worked on all kinds of projects, which has allowed me to learn and explore new strategies to achieve top rankings in search engines.

Related news

Hello! drop us a line