1. Regular Expressions: The Basics

Literals, metacharacters, and the dot

Regular expressions — regexes — are patterns that describe sets of strings. You hand a regex to an engine, point it at some text, and it finds every substring that matches the pattern. They show up everywhere: search-and-replace in editors, input validation on the web, log parsing on servers, and route matching in frameworks.

A regex can be as simple as a literal word or as intricate as a multi-line pattern with lookaheads, backreferences, and conditional branches. This series starts simple and builds up.

Literal Characters

The most basic regex is just the text you want to find. The pattern cat matches the substring "cat" wherever it appears.

Pattern

cat

Input

The cat sat on the concatenation mat.

Matches

The cat sat on the concatenation mat.

Notice that cat also matched inside "concatenation." Regex engines find every occurrence by default — they don't care about word boundaries unless you tell them to (we'll cover that in a later article).

Literal matching is case-sensitive. cat does not match "Cat" or "CAT" unless you enable the case-insensitive flag.

Metacharacters

Certain characters have special meaning inside a regex. These are the metacharacters:

. ^ $ * + ? { } [ ] ( ) | \

Each one controls how the pattern matches. We'll cover them all over the course of this series, but here's a quick overview:

Metacharacter Purpose
. Match any single character (except newline)
^ Anchor to the start of a line
$ Anchor to the end of a line
* + ? Quantifiers (how many times to repeat)
{n,m} Quantifier with explicit bounds
[...] Character class (match one of these)
(...) Group (capture or structure)
| Alternation (this OR that)
\ Escape the next character

If you want to match a metacharacter literally — for example, to find an actual period in text — you escape it with a backslash.

The Dot: Match (Almost) Anything

The dot . is the simplest metacharacter. It matches any single character except a newline (\n).

Pattern

c.t

Input

cat cot cut c0t c_t c t

Matches

cat cot cut c0t c_t c t

The dot matched a, o, u, 0, _, and even a space — any single character between c and t. This makes the dot a powerful but blunt tool. Use it when you genuinely don't care what the character is; use a character class when you need precision.

Dotall Mode

Some regex flavors support a dotall or single-line flag (often s) that makes the dot match newlines too. Without it, . stops at line boundaries:

Pattern (no dotall)

start.end

Input

start
end

Result

No match — the . won't cross the newline.

Pattern (with dotall flag /s)

start.end

Now it matches, because . also matches \n.

Escaping Special Characters

When you need to match a character that happens to be a metacharacter, prefix it with a backslash \.

Pattern

price: \$\d+\.\d{2}

Input

The price: $9.99 is final.

Matches

The price: $9.99 is final.

Here \$ matches a literal dollar sign and \. matches a literal period. Without the backslashes, $ would mean "end of line" and . would mean "any character."

Common Escape Sequences

Beyond escaping metacharacters, the backslash introduces several shorthand sequences that represent common characters:

Sequence Matches
\n Newline
\t Tab
\r Carriage return
\d Any digit (same as [0-9])
\w Any word character (same as [a-zA-Z0-9_])
\s Any whitespace (space, tab, newline, etc.)
\D Any non-digit
\W Any non-word character
\S Any non-whitespace

We'll explore \d, \w, and \s in depth in the next article on character classes.

Putting It Together

Let's trace through a slightly more involved pattern to see how these basics combine.

Pattern

Dr\. .+

Breakdown

  • Dr — literal "Dr"
  • \. — literal period (escaped)
  • — literal space
  • .+ — one or more of any character

Input

Dr. Alice Smith graduated in 2019.

Matches

Dr. Alice Smith graduated in 2019.

The .+ is greedy — it gobbles up as many characters as possible. That's why it matched all the way to the end of the line, not just "Alice." We'll learn how to control greediness in the quantifiers article.

Key Takeaways

  • A regex is a pattern that describes a set of strings.
  • Literal characters match themselves.
  • Metacharacters (. ^ $ * + ? { } [ ] ( ) | \) have special meaning.
  • The dot . matches any single character except newline (unless dotall mode is on).
  • Escape metacharacters with \ to match them literally.
  • Shorthand sequences like \d, \w, and \s are convenient aliases for common character classes.

Next up: character classes — how to match specific sets of characters with [...] and the shorthand escapes.