Regex Case Insensitive and Word Boundary in Search Console

About 24 minutes

In the last lessons we used ordinary English words in our regex: we wrote (price|cost|how much) and ^(how|can|why). But the problem starts when you look at real Search Console data. You might find "optimise images" and "optimize images", "wifi router" and "wi-fi router", "iphone 17 colours" and "iphone 17 colors". So the very thing you're trying to isolate, users can type in more than one way. Today we won't learn a different regex. It's the same regex we know, but we'll learn to use it with the way people actually type, without turning every pattern into a huge list of options.

Square brackets []

You have: optimise images, optimize images, compress images, image size. The difference between "optimise" (UK) and "optimize" (US) is one letter, s or z. Do I have to write both words in full every time? No. When we write [sz], it means: pick one character from the ones inside the brackets. So we can write optimi[sz]e instead of (optimise|optimize).

optimi[sz]e
  • optimise images
  • optimize images
  • compress images
  • image size
Watch out
[sz] is completely different from (s|z). In this simple example they may reach the same result, but [] is for picking one character from a set of characters, not a word. Look at [price|cost]: it's wrong if you mean "price or cost", because [] picks one character, not a word. The right way is (price|cost).

A character class can also hold a space and a hyphen. People type the same product as "wifi", "wi-fi" and "wi fi". The middle is a hyphen, a space, or nothing at all. So: [- ] picks a hyphen or a space, and the ? from lesson 4 makes it optional: wi[- ]?fi.

wi[- ]?fi
  • wifi router
  • wi-fi router
  • wi fi router
  • 5g router
The rule
Don't normalize just because you can. Don't say "from today, every s I write will be [sz]". Use it when you have a reason: you actually found a real variation in Search Console. If there's no real variation in the data, don't add complexity for nothing. Same rule as at the start of the course: the simplest pattern that solves the problem.

The same idea works for other spelling pairs. The ? on its own covers a letter that's sometimes there: colou?r matches "colour" and "color", and e-?mail matches "email" and "e-mail". But careful: we're not correcting spelling, and we're not saying "every s is a z" in general. We're using a character class or an optional letter in one particular place, inside one particular word, because we noticed a real variation.

Note
This is the most important mindset for messy queries: we're not trying to make regex understand English. We're telling it: in this exact spot, more than one character can appear.

() versus []

If I have whole words (optimize, compress, speed up), I use (optimi[sz]e|compress|speed up) style alternation. If I have a different character in the same spot (optimise, optimize), I use optimi[sz]e. And we can combine them: (optimi[sz]e|compress|speed up) is a group of words, and inside one of them we used a character class to pick the letter.

(optimi[sz]e|compress|speed up)
  • optimise images
  • optimize images
  • compress images
  • speed up wordpress
  • image size

Now let's try to match the whole word

Say you want to segment the "pro" models: iphone 17 pro, macbook pro. You write pro. Look at what comes back, because the match is partial:

pro
  • iphone 17 pro
  • iphone 17 pro max
  • pro-level camera phone
  • product photography phone
  • iphone promo code
  • improve iphone battery

"product", "promo" and "improve" all contain the letters p-r-o. That's the classic problem with short words in English: app is inside "apple" and "happy", ai is inside "email" and "repair". We need to say: the word pro on its own, not pro inside another word.

This is where \b comes in: a word boundary. It doesn't match a character; it matches the edge between a word character and something that isn't one (a space, a hyphen, the start or end of the text). The RE2 engine Search Console uses defines \w, a word character, as A to Z, a to z, 0 to 9 and the underscore. So \bpro\b means: pro, with no letter or digit glued to either side.

\bpro\b
  • iphone 17 pro
  • iphone 17 pro max
  • pro-level camera phone
  • product photography phone
  • iphone promo code
  • improve iphone battery

Now "product", "promo" and "improve" are gone. But notice "pro-level camera phone" stayed: a hyphen isn't a word character, so it counts as a boundary. Is that what you want? Maybe, maybe not. That's exactly the kind of case to check against your real data, not to assume.

Watch out
\b only works this way because these are English letters. In RE2, letters from other scripts, like Arabic, aren't word characters at all, so \b finds no edges around them, and a pattern like \bprice\b copied to another language can silently match nothing. If your site also gets queries in another language, test \b on that data before you rely on it. (The Arabic edition of this course has a whole lesson on exactly that.)

Another way to say "the whole word", using only what we already know: the word comes at the start of the query or after a space, and is followed by a space or the end of the query: (^|\s)pro(\s|$). It's stricter than \b: it drops "pro-level" too, because a hyphen isn't a space.

(^|\s)pro(\s|$)
  • iphone 17 pro
  • iphone 17 pro max
  • pro-level camera phone
  • product photography phone
  • iphone promo code
  • improve iphone battery

Finding queries with characters outside basic English

Some queries carry accents or characters from other languages: "café near me" next to "cafe near me", "crème brûlée recipe" next to "creme brulee recipe". Inside square brackets, a ^ at the very start flips the meaning: any character that's not in the set. And \x00-\x7F is the range of basic English characters (ASCII). So [^\x00-\x7F] means: one character that isn't basic English.

Predict the result

Here's [^\x00-\x7F]. How many of these four queries contain at least one character outside basic English?

[^\x00-\x7F]
  • cafe near me
  • café near me
  • creme brulee recipe
  • crème brûlée recipe
Note
Watch the meaning: [^\x00-\x7F] doesn't mean "a query with an accent". It means "a query with at least one non-ASCII character": an accented letter, a curly apostrophe pasted from a phone, an emoji, or a word in another script. That makes it useful for discovering what's in your data, and then you look at what it found.

And if you want the queries that do contain English letters? [A-Za-z]: a character from A to Z or a to z.

Two words in order, with anything between them

Say you want the queries that mention an iphone and then a case: "iphone 17 pro case", "iphone silicone case". First a small new symbol: * means zero or more (compared with +, which means one or more). With . we can write .*, meaning "any number of characters, even zero".

So iphone.*case means: iphone, then anything, then case. The order matters: iphone has to come first.

iphone.*case
  • iphone 17 pro case
  • iphone silicone case
  • case for iphone 17
  • iphone 17 price

"case for iphone 17" didn't show up, because there case comes before iphone. If you want both orders, combine them: (iphone.*case|case.*iphone). That's useful when you want to understand how people word things: do they lead with the product or with the accessory? Is there a CTR difference between the two phrasings?

Do I have to turn every variation into a clever regex?

No. If you try to compress every letter, every variation and every typo, you can end up with a pattern nobody understands. Sometimes (tech zone|techzone|tech-zone|teck zone) is clearer and safer than a very clever regex. The test isn't "who wrote the shortest regex?" but "is the pattern clear, and does it produce the right segment?"

Watch out
If the correct brand name is "adidas" but users type "addidas", it isn't your job to say "that's a typo, I won't count it". We're analyzing search behavior: if people type it, it's part of the data. But the same rule applies: don't guess the typos. Start with what you know, review Search Console, see the real variations, then add them.
Try it yourself

You have: iphone 17 colours, iphone 17 colors, iphone 17 size. You want both spellings of colour, and nothing else.

  • iphone 17 colours
  • iphone 17 colors
  • iphone 17 size
Try it yourself

You have: wifi extender, wi-fi extender, wi fi extender, best router. Write a pattern that covers the three ways of typing wifi.

  • wifi extender
  • wi-fi extender
  • wi fi extender
  • best router

Remember: this is a pattern for this word, not a general rule that every hyphen is optional.

Try it yourself

You have: smart shop, smartshop, smart-shop, and the typo smrt shop. Build a brand pattern.

  • smart shop
  • smartshop
  • smart-shop
  • smrt shop

The practical test

You have a brand called Smart Store, and users type it as: smart store, smartstore, smart-store, and the typo smrt store. Careful: the site also gets queries like "smartphone store", which isn't the brand.

Regex challenge

A brand regex for Smart Store

Build a pattern that covers the four spellings, and leaves out every other query.

Must match
  • · smart store
  • · smartstore
  • · smart-store
  • · smrt store
Must not match
  • · iphone 17
  • · iphone price
  • · smartphone store

A thinking test

If you have a brand that people type 8 different ways, what's the first thing you do? Not "write the cleverest regex possible", but "understand the real variations in the data". Then you decide: is the difference one character? Use []. Do I have different words or spellings? Use (). Is a small part optional? Maybe ?. Is a short word matching inside longer ones? Think about \b. What matters is that the segment is clear and correct.

Before the next lesson

So far we've worked mainly on queries. But Search Console lets us use regex on pages too. In the next lesson we move the same mindset to the structure of the site. We'll see how to isolate the blog, products, categories, services, different languages, PDF files, pagination, and URLs with parameters, years or IDs. And that's where regex starts helping us understand not only how people search, but also which parts of the site get the visibility and the traffic in Search Console.

Well done, you reached the end of the lesson.