Regular Expressions for Beginners

By the ToolPocket team · Updated 2026-12-05 · Editorial standards

Regular expressions look like line noise until the basic pieces click. Most real-world patterns use only a small subset.

A regular expression is a pattern that describes a set of strings. The engine walks through your text trying to match that pattern. Once you know five building blocks, most everyday tasks become manageable.

1. Literals and character classes

Most characters match themselves: cat matches "cat". Square brackets define a set: [aeiou] matches any single vowel, and [0-9] matches a digit. A negated class, [^0-9], matches anything that is not a digit.

2. Quantifiers

+ means one or more, * means zero or more, ? means optional, and {2,4} means two to four repetitions. Quantifiers apply to the thing immediately before them, so ab+ matches "ab", "abb" and so on, while (ab)+ matches repeated "ab" pairs.

3. Anchors

^ matches the start of a line and $ the end. With the m flag they apply to every line, not just the whole string. Anchors are how you say "the entire line looks like this" instead of "somewhere in the text".

4. Groups and capture

Parentheses group parts of a pattern and capture what they match. In (\d{4})-(\d{2})-(\d{2}), the three groups capture the year, month and day separately. Named groups, (?<year>\d{4}), make the result easier to read in code.

5. Escaping

Characters such as ., *, +, ?, (, ), [ and ] have special meanings. To match them literally, put a backslash in front: \. matches a real dot.

A few traps

Practise safely

Use the regex tester to try patterns against sample text before they reach production. It shows every match, its position and the capture groups, which is usually enough to spot the mistake.

← All guides