Kolam Ayer MakersLinux Foundations

Regular Expression

Core Idea

A regular expression is a pattern for matching text.

grep, sed, and many other Unix tools use regular expressions to find the lines or parts of lines that matter. A regular expression is not the same thing as a shell glob.

First Patterns

Literal text:

grep 'makers' ~/src/pages/index.md

Line starts with #:

grep '^#' ~/src/pages/index.md

Line ends with .org:

grep '\.org$' ~/src/pages/index.md

Any character:

grep 'h.t' words.txt

Zero or more of the previous thing:

grep 'lo*l' words.txt

Small Vocabulary

Pattern Meaning
abc Literal text abc.
. Any one character.
* Zero or more of the previous atom.
^ Start of line.
$ End of line.
[abc] One character: a, b, or c.
[^abc] One character that is not a, b, or c.
\. Literal dot.

Regex Is Not Shell Glob

Shell glob:

ls *.md

The shell expands *.md into matching filenames before ls runs.

Regular expression:

grep '\.md$' file-list.txt

grep receives the pattern and matches it against text lines.

Basic Versus Extended

Different tools use slightly different regex dialects.

In this course:

This is why the course sed example looks like this:

sed 's/^# \(.*\)$/<h1>\1<\/h1>/'

Common Failures

Proof Check

Create a scratch file with headings, ordinary lines, and a URL. Use grep '^#', grep '\.org$', and one sed substitution. Explain which characters were literal and which were regex syntax.

Docs Pointers