Regex Starts With and Ends With in Search Console (^ and $)
About 22 minutes
In the last lesson we wrote our first simple regex with |, meaning "or", and we learned to use () to group several options together, like (price|cost|how much). Search Console brought back any query containing one of those words.
But today we have a different problem. Say you have these queries:
- iphone
- iphone 17
- iphone price
- best iphone
- buy iphone
- iphone uk
If you use iphone, almost everything with the word iphone shows up, whether it's at the start, the middle or the end. But what if your question was: I want only the queries that start with iphone? Or the queries that end with iphone? Or the query that is just iphone, with nothing before or after it?
Here, just writing iphone isn't enough. We need to start controlling where the text is. And that's what we'll learn today.
The first symbol: ^
The ^ symbol means "the start of the text". So if you write ^iphone, you're saying: the text has to start with iphone. Let's try:
^iphone- iphone
- iphone 17
- iphone price
- best iphone
- buy iphone
- iphone uk
The word iphone really is in "best iphone" and "buy iphone", but not at the start.
^iphone, read it as "the start of the text, then iphone", not "a strange symbol called a caret". The name doesn't matter right now; the meaning does.An example with question words
You have: how to choose a laptop, how to buy a laptop, best laptop for work, what is the best laptop, how to check my laptop specs. And you want: every query that starts with "how".
^how- how to choose a laptop
- how to buy a laptop
- best laptop for work
- what is the best laptop
- how to check my laptop specs
What if there's more than one start?
Say you want the queries that start with how, can or why. We can use what we learned in the last lesson:
- 1^ — the start of the text
- 2
( — a group - 3how
- 4| — or
- 5can
- 6| — or
- 7why
- 8
) — the end of the group
So the whole regex means: the text has to start with how, can or why.
^(how|can|why)- how to choose a laptop
- can a macbook run windows
- why is my laptop slow
- best laptop for coding
- laptop overheating why
Focus on the last one: "laptop overheating why" has the word "why" in it, but not at the start. So it didn't show up.
The second symbol: $
Now the opposite idea. The $ symbol means "the end of the text". If you write iphone$, it means: the text has to end with iphone.
iphone$- iphone
- buy iphone
- best iphone
- iphone 17
- iphone price
Because the last thing in the query has to be iphone.
A clear SEO example: say your site targets several countries, and you want the queries that end with uk:
uk$- iphone price uk
- iphone price us
- iphone price canada
- best iphone uk
- uk iphone price
The last one has uk in it, but at the start, not the end.
What if we use ^ and $ together?
This brings us to a very important idea. If you write ^iphone$, read it as: the start of the text, iphone, the end of the text. So there's nothing before iphone, and nothing after it. Which means the whole text has to be exactly iphone.
^iphone$- iphone
- iphone 17
- best iphone
- iphone price
And that's the first important difference that has to be clear in your head.
The difference between the three
We have 3 patterns, and each one is a different question. Let's try them on the same data:
iphone- iphone
- iphone 17
- best iphone
- buy iphone
- iphone price
^iphone- iphone
- iphone 17
- best iphone
- buy iphone
- iphone price
^iphone$- iphone
- iphone 17
- best iphone
- buy iphone
- iphone price
If that difference is clear to you, you've understood the most important part of the lesson.
So why does an exact match matter in SEO?
You might say: why would I need a query to be exactly one word? There are many uses. For example, you have a brand called "nova", and you want to know how the query "nova" itself performs. If you use nova, you might get:
- nova
- nova store
- buy nova
- nova uk
- best nova product
But if you only want the brand word:
^nova$- nova
- nova store
- buy nova
- nova uk
- best nova product
Now you're isolating exactly one query. That's very useful when you're trying to separate a pure brand query from a brand + modifier.
You want the query that is exactly the brand name, with nothing added. How many of these five will match ^nike$?
^nike$- nike
- nike shoes
- nike uk
- buy nike
- nike price
The difference matters: nike alone says "the person typed just the brand name". nike plus something says "the person mentioned the brand in some form". And that difference can matter in the analysis.
Now something new: special characters
So far we've used ^ $ | (). These aren't ordinary characters; they're regex symbols with a meaning. Say you want to search for "example.com". Does the dot in regex really mean a dot? The answer: no. In regex, the dot . has a special meaning: roughly "any one character". So if you write example.com, you're not literally saying "example, then a dot, then com". You're saying "example, then any character, then com".
Let's see it with an example. The regex a.b can match:
a.b- a.b
- acb
- a-b
- a1b
Because . means "any one character".
So what if I want an actual dot?
That's when we use \., meaning: I don't want the dot's special regex meaning; I want the dot character itself. For example, example\.com means "example.com" literally. The \ symbol is called an escape, but as we agreed, the name isn't the most important thing. Think of it like this: don't treat the character after me as a regex symbol; treat it as an ordinary character.
example\.com- example.com
- exampleXcom
Why does this matter in Search Console? Because you'll work with URLs a lot. For example, if you want a precise pattern for a domain, notice \. instead of ., because the dot is a real part of the domain name. Don't try to memorize a big list of symbols: we'll learn each symbol when we need it.
Applying it to pages instead of queries
So far most examples have been queries. Let's start seeing that the same thinking works on pages. You want every page inside the blog:
^https://example\.com/blog/https://example.com/blog/https://example.com/blog/python-seo/https://example.com/https://example.com/product/laptop/https://example.com/services/
Now ^ has become a tool for understanding the site's structure, not just a symbol we memorized. Another example, the products section:
^https://shop\.com/product/https://shop.com/product/iphone/https://shop.com/product/samsung/https://shop.com/category/phones/https://shop.com/blog/iphone-review/
A common mistake: I wanted one URL and only used the start of the text
Say you want the page https://example.com/blog/ exactly. If you write ^https://example\.com/blog/, these also show up:
^https://example\.com/blog/https://example.com/blog/https://example.com/blog/python/https://example.com/blog/seo/
Because you told it "start with this path", but you didn't say "and end here". If you want only that page:
^https://example\.com/blog/$https://example.com/blog/https://example.com/blog/python/https://example.com/blog/seo/
A very important example: the question word itself
You have: how to choose a laptop, how to buy a laptop, how to find a laptop's price, laptop how to choose one, best laptop.
how- how to choose a laptop
- how to buy a laptop
how to find a laptop's price- laptop how to choose one
- best laptop
^how- how to choose a laptop
- how to buy a laptop
how to find a laptop's price- laptop how to choose one
- best laptop
That difference matters, because you're trying to describe the shape of a query, not just whether a word is in it.
Putting lessons 1 and 2 together
You want the queries that start with one of: how, can, why, when. We use:
^(how|can|why|when)- how to choose a laptop
- can a macbook run windows
- why does my laptop overheat
- when to replace a laptop
- best laptop
- laptop worth it how
Notice we've started building regex out of small ideas we already understand.
An example that needs a bit of focus
We have the regex ^(iphone|samsung)$. Let's take it apart: the start of the text, then iphone or samsung, then the end of the text. So: the whole text is just iphone or samsung.
^(iphone|samsung)$- iphone
- samsung
- iphone 17
- best samsung
- samsung phone
If you understood that example without memorizing it, we're on the right track.
^(iphone|samsung): it means "the text starts with iphone or samsung". That's fine, but when we build more complex patterns later, the order of the symbols will matter a lot. So get into the habit now: don't read a regex as one block; take it apart piece by piece.- 1
^ = the start of the text - 2
( ) = a group - 3
| = or - 4
So ^(price|cost) = the text starts with price or cost - 5
And the other way round: (uk|canada)$ = the text ends with uk or canada
You have: how to learn seo, can i learn seo in a month, best seo course, seo how to learn it, why is seo important. You want: the queries that start with how, can or why.
- how to learn seo
- can i learn seo in a month
- best seo course
- seo how to learn it
- why is seo important
You have: iphone uk, iphone us, price iphone uk, uk iphone, iphone canada. You want: the queries that end with uk.
- iphone uk
- iphone us
- price iphone uk
- uk iphone
- iphone canada
You have: seo, seo course, learn seo, seo tools. You want: the query that is just seo.
- seo
- seo course
- learn seo
- seo tools
You have these pages: https://example.com/blog/, https://example.com/blog/seo/, https://example.com/product/, https://example.com/blogger/. You want: the pages inside /blog/.
https://example.com/blog/https://example.com/blog/seo/https://example.com/product/https://example.com/blogger/
Notice that /blogger/ isn't the same as /blog/, a good example of why precision matters.
The practical test
You have: how to choose a macbook, can a macbook run windows, best macbook for programming, macbook how to reset, macbook, macbook pro, buy macbook, why is the macbook so expensive.
Queries that start with how, can or why
Write a regex for the queries that start with how, can or why.
- · how to choose a macbook
- · can a macbook run windows
- · why is the macbook so expensive
- · best macbook for programming
- · macbook how to reset
- · macbook
- · macbook pro
- · buy macbook
Write a regex that shows only macbook, without macbook pro or buy macbook.
- macbook
- macbook pro
- buy macbook
You have the URL https://example.com/blog/. Write a pattern that matches only that page, without the article pages under it.
https://example.com/blog/https://example.com/blog/python-seo/
Explain why example.com isn't the most precise choice if you mean the real dot in the domain name: you need to escape it, example\.com.
A thinking test
If you want every query that starts with the word "best", what's the first question you need to ask? Not "how do I write the regex?" but: "do I care about the word best being anywhere, or about the shape of a query that starts with it?" Because best and ^best answer two different questions.
Before the next lesson
So far we have |, (), ^, $ and \.. But more important than the symbols, you can now tell apart "contains", "starts with", "ends with" and "equals the whole text".
In the next lesson we'll face a completely different problem. We won't even know the word we're looking for. We'll ask, for example: I want the queries made of 5 words or more, or 7 words or more, or 10 words or more. And that's where, for the first time, we'll build a regex based on the shape of the query and its number of words, not on words we know in advance.
Well done, you reached the end of the lesson.