Search

INPUT · Slides

Search by shape (regular expressions)

01 / 09

Some things you cannot put into words

The grep from the last lesson looks for "lines with this word in them". But what you are after cannot always be put as a word.

  • "the lines with a phone number on them"
  • "the lines that start with a date"
  • "the bits that look like an email address"

That is a shape, not a word. The way of writing a shape down is a regular expression.

02 / 09

A regular expression is symbols for the shape of text

Regular expressions do not belong to grep. They are in sed, in awk, in the search box of your editor, and in JavaScript and Python too. Learn them once and you can use them everywhere.

There are only six to learn here, and they cover eight jobs out of ten.

SymbolMeaning
^the start of a line
$the end of a line
.any one character
[abc]any one of these
[^abc]any one character that is not these
*repeat what is in front (zero or more)

03 / 09

^ and $ — pinning the ends

These two get used the most.

  • ^s — lines that start with s
  • com$ — lines that end with com

Without a pin it becomes "anywhere in the line will do". grep sato also matches the sato inside [email protected], but ^sato only matches the name at the start.

The more you pin, the more precise it gets. That idea sits at the centre of regular expressions.

~ $ grep '^s' contacts.txtsuzuki 080-3333-4444 [email protected]sato 03-5555-6666 [email protected]

04 / 09

. and [] — a one character hole

. is any one character. c.t matches cat and cut alike.

[] is stricter: "any one of these". [sy] is s or y. You can write ranges too.

  • [0-9] — one digit
  • [a-z] — one lower case letter
  • [A-Za-z] — one letter

Put a ^ inside, as in [^0-9], and it becomes anything but. That is a different meaning from the ^ outside the brackets, so watch out.

~ $ grep '090-....-....' contacts.txttanaka 090-1111-2222 [email protected]yamada 090-7777-8888

05 / 09

* and .*

* is the mark for repeat what is in front of it. Zero times counts, so ab*c matches both ac and abbbc.

The combination you use most is .*. "Any one character", "as many as you like", which comes to any run at all.

grep '090.*com' contacts.txt

That is "lines with 090 on them and, somewhere after it, com". It looks like the * from find, but it means something different. A regular expression * cannot stand on its own — you have to write .*.

06 / 09

-E makes it easier to write

That was the basic set. There are some handier symbols, but you need -E to use them.

SymbolMeaning
+repeat what is in front, once or more
?what is in front may or may not be there
`\`one or the other
()group things together
{3}exactly three times

Without -E you cannot write these (you need a \ every time, as in \{3\}), so when in doubt, add -E.

~ $ grep -E '^[0-9]{4}-[0-9]{2}-[0-9]{2}' records.txt2026-08-21 10:00 start2026-08-21 10:05 error

07 / 09

Searching for the symbol itself

. means "any one character", which is awkward when you want an actual dot.

When that happens, put a \ in front to say "this is a character, not a symbol". That is called escaping.

grep '1\.2\.3' records.txt     # an actual dotgrep '1.2.3' records.txt       # matches 1x2x3 as well

The ones that need escaping are . * [ ] ^ $ \ and friends. Forget it and the symptom is "somehow extra things come out", so when you get too much, suspect the escaping.

08 / 09

Why you wrap it in quotes

You always wrap a regular expression in quotes.

$, * and ? are special to the shell too, so without quotes the shell gets its hands on them before grep ever sees them.

grep 'com$' file      # goodgrep com$ file        # may not do what you meant

Single quotes ' are the ones to reach for. With double quotes " the $ can still get eaten by the shell (more on that in the chapter on the shell environment).

09 / 09

Now have a go

contacts.txt holds five people. Four have a phone number, four have an email, and three have both.

records.txt holds records where the dates are written every which way. Proper ones like 2026-08-21 are mixed in with short ones like 2026-8-21.

Let us get sorting by shape into your hands.