Regex is the cheapest leverage in SEO. One pattern in Search Console can replace an hour of spreadsheet filtering.
Everything below is copy-paste. Steal it.
Know which engine you’re in
Patterns that work in one tool break in another. This catches most people out.
- Search Console, GA4, Looker Studio: RE2. No lookaheads, no lookbehinds, no backreferences.
- Screaming Frog, most code and redirect rules: Java or PCRE-style. Lookaheads work.
- Search Console is case-sensitive by default. Put
(?i)at the start to switch that off.
If a pattern errors, check the engine before you check the pattern.
The five bits of syntax you actually need
| Symbol | Meaning |
|---|---|
. | Any one character |
* + ? | Zero or more, one or more, optional |
^ $ | Start and end of the string |
| | Or |
\ | Escape the next character |
That covers most real work. Add \b (word boundary), \s (space) and \S (not a space) and you’re done.
Search Console: query filters
Brand queries (case-insensitive, with misspellings)
(?i)(yourbrand|your brand|yourbrnad)
Use “Custom (regex)” to include them. Use “Doesn’t match regex” to exclude them. RE2 has no negative lookahead, so exclusion is a filter setting, not a pattern.
Question queries
(?i)^(who|what|when|where|why|how|can|does|do|is|are|should|which)\b
Long queries (6+ words)
^(\S+\s){5,}\S+$
Change the number to move the threshold. Long queries are where AI search behaviour shows up first.
Comparison and decision queries
(?i)\b(vs|versus|compare|comparison|alternative|alternatives|best|review|reviews)\b
Queries with a year
20[12][0-9]
Search Console: page filters
A folder, nothing else
^https://example\.com/blog/
Anything with parameters
\?
Pages at exactly one level deep
^https://example\.com/[^/]+/?$
The [^/]+ bit means “anything except a slash”. It’s the pattern most people never learn and then use daily.
GA4: AI referral traffic
Create a channel group or exploration filter on session source:
(?i)(chatgpt\.com|perplexity|gemini\.google\.com|copilot\.microsoft\.com|claude\.ai)
Escape the dots. Unescaped, chatgpt.com also matches chatgptXcom. Harmless here, wrong everywhere else.
Referrer strings change. Check your actual source list quarterly and update the pattern.
Log files: AI crawlers
(?i)(GPTBot|ChatGPT-User|OAI-SearchBot|ClaudeBot|Claude-User|PerplexityBot|CCBot|Bytespider)
Google-Extended is a robots.txt token, not a user agent. It won’t appear in your logs. Googlebot does the fetching.
Also: user agents can be spoofed. A match tells you what the request claimed to be. Verify against published IP ranges before you draw conclusions.
Redirects
Move a folder, keep the slug
^/blog/([^/]+)/?$ → /articles/$1
Strip a trailing index file
^(.*)/index\.html$ → $1/
Always test with ten real URLs, including one with a trailing slash and one with a query string. A redirect rule that works on the happy path is how you lose a section.
Screaming Frog: custom extraction and search
Screaming Frog uses a more forgiving engine, so lookaheads work.
Pages with a title over 60 characters
^.{61,}$
Internal links to old URLs
href="(/old-section/[^"]*)"
Pages containing a phrase but not another
^(?!.*discontinued).*in stock
Four mistakes that cause most of the damage
- Unescaped dots.
.means any character. Write\.when you mean a dot. - Greedy matching.
.*grabs as much as it can. Use[^"]*or.*?when you want the nearest match. - Missing anchors. Without
^and$,/shoes/also matches/womens-shoes/sale/. Anchor it. - Skipping the test. Paste your pattern and ten real strings into a tester before it touches a live redirect. Test the strings that should not match as well.
A sensible way to learn it
Don’t read a regex textbook. Take one report you already filter by hand every week. Write the pattern that does it. Save it. Repeat next week.
In a month you’ll have a personal library, and you’ll stop opening spreadsheets to filter things.
Patterns tested against the documented behaviour of each tool. Tool syntax changes, so re-check if something stops working.