Regex Cheat Sheet: Python and Google Search Console

Every regex symbol you'll use, with an example, and the column most cheat sheets leave out: whether it works in Google Search Console, which runs Google's RE2 engine, and in Python's re module. Then the patterns SEOs use most, ready to paste into a Search Console filter.

Characters and alternatives

SymbolMeansExampleSearch ConsolePython
abcThese characters, anywhere in the textseomatches seo tools, best seo✓ works✓ works
.Any one characters.omatches seo, sko✓ works✓ works
\.A real dot (escape a symbol with \)example\.com✓ works✓ works
|Orprice|cost✓ works✓ works
( )A group(cheap|best) laptop✓ works✓ works
(?: )A group that doesn't capture(?:cheap|best) laptop✓ works✓ works

Learn it with exercises: Search Console regex filter: match several keywords at once

Start, end and word boundaries

SymbolMeansExampleSearch ConsolePython
^Start of the text^howmatches how to…, not why how✓ works✓ works
$End of the text\.pdf$URLs ending in .pdf✓ works✓ works
\bA word boundary\bpro\bpro, not product✓ works✓ works
Note

In Search Console, \b, \d, \w and \s only know plain English (ASCII) characters, so \b can't find the edge of an Arabic or accented word. In Python, they know every alphabet.

Learn it with exercises: Regex starts with and ends with in Search Console

Character classes

SymbolMeansExampleSearch ConsolePython
[abc]One character from the listoptimi[sz]eoptimise or optimize✓ works✓ works
[^abc]One character not in the list[^a-z0-9 ]a symbol or accent✓ works✓ works
[a-z]One character in a range[0-9]any digit✓ works✓ works
\dA digitiphone \d+iphone 17✓ works✓ works
\wA letter, digit or _\w+✓ works✓ works
\sA spaceseo\s+tools✓ works✓ works
\SAnything but a space^\S+$one-word queries✓ works✓ works
\p{L}Any letter, in any alphabet\p{Arabic}Arabic queries✓ works✕ doesn't work
Note

Python's built-in re doesn't know \p{…}. The third-party regex package does.

Learn it with exercises: Regex case insensitive and word boundary in Search Console

How many times

SymbolMeansExampleSearch ConsolePython
*Zero or moreiphone.*caseiphone, then case later✓ works✓ works
+One or more\d+17, 2026✓ works✓ works
?Zero or onecolou?rcolor or colour✓ works✓ works
{n}Exactly n times\d{4}a year✓ works✓ works
{n,}n times or more^\S+(\s+\S+){6,}$7+ word queries✓ works✓ works
{n,m}Between n and m times^\S+(\s+\S+){3,5}$4 to 6 words✓ works✓ works
*?As few as possible<.*?>one tag, not the whole line✓ works✓ works

Learn it with exercises: Find long-tail queries in Search Console with regex

Letter case

SymbolMeansExampleSearch ConsolePython
(?i)Ignore letter case(?i)seoSEO, Seo, seo✓ works✓ works
(?-i)Respect letter case from here on(?-i)[A-Z]URLs with capitals✓ worksPartly

The defaults are opposite. Search Console ignores letter case unless you start with (?-i). Python respects it unless you add (?i) or re.IGNORECASE; Python only accepts (?-i) inside a group, like (?-i:Seo).

Learn it with exercises: Search Console regex for pages

Works in Python, not in Search Console

SymbolMeansExampleSearch ConsolePython
(?=…)Followed by…iphone(?=.*price)✕ doesn't work✓ works
(?!…)Not followed by…^(?!.*repair).*iphone✕ doesn't work✓ works
(?<=…)Preceded by…(?<=/blog/)\w+✕ doesn't work✓ works
(?<!…)Not preceded by…(?<!no )sale✕ doesn't work✓ works
\1The same text as group 1 again(\w+) \1repeated words✕ doesn't work✓ works
Watch out

Search Console runs RE2, which leaves these out on purpose. A pattern with one of them doesn't throw an error you can see: it just returns nothing. To include one thing and exclude another, use two filters: Matches regex for what you want, and Doesn't match regex for what you don't.

Learn it with exercises: Search Console regex not working? How to test and fix it

Regex patterns for SEO in Search Console

TaskPattern
A brand and its misspellings(smart[- ]?store|smrt store)
Non-brand queriesthe brand pattern, with Doesn't match regex
Questions^(how|what|why|when|where|which|can|is|does)\b
Comparisons(vs|versus|difference between|compare)
Price queries(price|pricing|cost|how much|cheap)
Long-tail queries, 7+ words^\S+(\s+\S+){6,}$
Queries with a year20\d{2}
One section of the site^https://example\.com/blog/
PDF files\.pdf$
URLs with parameters\?
URLs with capital letters(?-i)[A-Z]

In Search Console, "Matches regex" finds the pattern anywhere in the query or URL, the way Python's re.search() does. To match the whole text, anchor it with ^ and $, or use re.fullmatch() in Python.

Each of these is built and tested step by step in the free Google Search Console regex course.