regex (Regular Expressions)

Regular expressions are a powerful tool to do pattern based selection. It uses special notations similar to those we've encountered already such as the * wildcard.

We'll go through a couple of the most common regular expressions, these are almost universal with any programming language.

We'll use this phrase as our test string. Make it into a file so you can actually try these:

$ printf 'sally sells seashells\nby the seashore\n' > tongue.txt

Each example below can be run as grep with the pattern in single quotes, like this:

$ grep 'sea' tongue.txt
sally sells seashells 
by the seashore

Quote the pattern. Several of the characters below mean something to the shell as well, and the quotes keep them safe until grep sees them.

1. Beginning of a line with ^

^by
would match the line "by the seashore"

2. End of a line with $

seashore$
would match the line "by the seashore"

3. Matching any single character with .

b.
would match by

4. Bracket notation with [] and ()

This can be a little tricky, brackets allow us to specify characters found within the bracket.

d[iou]g
would match: dig, dog, dug

The previous anchor tag ^ when used in a bracket means anything except the characters within the bracket.

d[^i]g
would match: dog and dug but not dig

Brackets can also use ranges to increase the amount of characters you want to use.

d[a-c]g
will match patterns like dag, dbg, and dcg

Be careful though as brackets are case sensitive:

d[A-C]g
will match dAg, dBg and dCg but not dag, dbg and dcg

And those are some basic regular expressions. There is a great deal more: repetition with * + and ?, alternation with |, grouping with parentheses, and a frankly confusing split between "basic" and "extended" syntax, which is why grep -E sometimes behaves differently from plain grep.

This lesson is a starting point rather than a reference. Two places are worth knowing when you need more:

  • RegexOne is a short interactive tutorial that takes about an hour and covers the rest of the syntax properly.
  • regex101 lets you paste a pattern and some text and watch it match, piece by piece, with an explanation of every character. Set the flavour to POSIX or PCRE depending on the tool you are using. For working out why a pattern does not do what you expected, nothing else comes close.

Exercise

Try each pattern from this lesson against tongue.txt, in the form:

$ grep '^by' tongue.txt
$ grep 'seashore$' tongue.txt
$ grep 'b.' tongue.txt

The square brackets in this exercise's earlier wording were a placeholder meaning "put your own thing here". Do not type them, unless you mean the bracket notation from the lesson.

Quiz Question

What regular expression would you use to match a single character?

Show answer

.