Regular Expressions for Beginners: A Practical Introduction
A regular expression, often shortened to regex, is a compact pattern that describes a set of strings. Instead of searching for one exact word, you describe a shape: "three digits, a dash, then four digits," or "a word that ends in ing." Editors, command line tools and almost every programming language support regex, so learning the basics pays off in many places.
The syntax looks cryptic at first, but it is built from a handful of ideas. This article introduces them one at a time, with examples that you can paste into a regex tester and try.
Literal characters
The simplest pattern is plain text. The pattern cat matches the letters c, a, t in that order, wherever they appear. It would match inside concatenate as well as the standalone word, because a regex looks for the pattern anywhere in the text unless you say otherwise.
A few characters have special meaning and must be escaped with a backslash if you want them literally: . * + ? ( ) [ ] { } | ^ $ \. To match a real full stop, write \..
The dot and character classes
The dot . matches any single character, except usually a line break. The pattern c.t matches cat, cot and c9t.
Square brackets define a character class, which matches one character from a set.
[aeiou]matches any lowercase vowel.[0-9]matches any digit, and[a-z]any lowercase letter.[^0-9]with a caret inside the brackets matches anything that is not a digit.
Shorthand classes save typing. In most flavours, \d means a digit, \w means a letter, digit or underscore, and \s means whitespace. The capital forms, \D, \W and \S, mean the opposite.
Quantifiers
A quantifier says how many times the previous item may repeat.
*means zero or more.+means one or more.?means zero or one, so the item is optional.{3}means exactly three, and{2,4}means two to four.
So \d+ matches a whole number of any length, and colou?r matches both color and colour. The pattern \d{3}-\d{4} matches a shape such as 555-0142.
Quantifiers are greedy by default, meaning they take as much text as they can. Adding a question mark after one makes it lazy. Compare these on the text <b>one</b><b>two</b>: the greedy <b>.*</b> matches the entire string, while the lazy <b>.*?</b> matches each tag pair separately.
Anchors and boundaries
Anchors match positions rather than characters.
^matches the start of a line, and$matches the end.\bmatches a word boundary, the edge between a word character and a non word character.
Anchors tighten a loose pattern. The pattern \d+ finds digits anywhere, while ^\d+$ accepts a line only if it consists entirely of digits. And \bcat\b matches the word cat but not concatenate, which solves the earlier problem.
Groups and alternation
Parentheses group part of a pattern, so a quantifier can apply to the whole group, and they also capture the matched text for later use.
(\d{4})-(\d{2})-(\d{2})
Against 2026-11-27, this pattern captures the year, month and day as groups one, two and three. Most languages let you read those groups, and many let you reuse them in a replacement, usually written with a dollar sign or a backslash followed by the group number.
The vertical bar | means "or." The pattern cat|dog matches either word, and gr(a|e)y matches gray and grey. If you do not need to capture a group, a non capturing form (?:...) avoids storing it.
A few practical patterns
Here are patterns you can adapt. Treat them as starting points, because real data is always messier.
^\s+|\s+$ leading or trailing whitespace
\b\d{1,3}(\.\d{1,3}){3}\b something shaped like an IPv4 address
^[\w.+-]+@[\w-]+\.[\w.-]+$ a rough email shape
#[0-9a-fA-F]{6}\b a six digit hex color
The email pattern shows an important lesson. A regex can check the rough shape of text, but it cannot prove that an address is real or fully valid under every rule. Use simple patterns for simple jobs, and use a proper parser when the format is complicated, such as HTML or nested data.
Flags
Most engines support flags that change behaviour. The common ones are i for case insensitive matching, g for finding all matches rather than the first, and m to let ^ and $ work on each line. Flags are written differently in each tool, so check your documentation.
Tips for learning and debugging
- Build patterns in small steps. Match one part, confirm it works, then extend it.
- Use an online or editor based tester that highlights matches as you type.
- Test against text that should not match as well as text that should.
- Remember that regex flavours differ. Features such as lookbehind and named groups vary between tools.
- Add a comment or a short example near any pattern you commit to code, since regex is hard to read later.
Frequently Asked Questions
What is the difference between greedy and lazy matching?
A greedy quantifier matches as much text as possible while still allowing the whole pattern to succeed. A lazy quantifier, written with a trailing question mark, matches as little as possible. Lazy matching is useful when you want the shortest span between two markers.
Can regex parse HTML or JSON?
It can pull out simple pieces, but it is the wrong tool for full parsing because those formats nest. Use a real HTML or JSON parser for anything beyond a quick extraction.
Why does my pattern work in one tool but not another?
Regex has several flavours, such as POSIX, PCRE and the version in JavaScript. They share the core ideas, but extras like lookbehind, named groups and shorthand classes can differ. Check which flavour your tool uses.