🛠️ The Most Useful Site in the World ← Back to the tools
Code & data · Definitive guide

Regular expressions without the pain — a 5-minute practical guide

A regular expression (regex) is a small language for matching patterns in text. Once you have learned the four characters that do 80% of the work, the rest of the language stops being scary. This guide covers the syntax, the four characters that matter most, a worked example of a real regex from this site's code, and the test-and-explain tool that tells you what a regex does.

⚡ TL;DR

The four characters that matter most, the syntax, and a worked example of a real regex from this site.

The four characters that matter most

The character class `[abc]` (matches one of the listed characters), the quantifier `+` (one or more of the previous element), the anchor `^` (start of the line) and `$` (end of the line). With those four, you can write a useful regex. Everything else is precision.

Anatomy of a regex

A regex is a pattern, optionally followed by flags. The pattern is a sequence of literal characters and metacharacters. Metacharacters are the things that mean something other than themselves: `.` (any single character), `*` (zero or more), `+` (one or more), `?` (zero or one), `[...]` (character class), `(...)` (group), `|` (alternation), `\` (escape). The flags are single letters after the closing `/` that modify behaviour: `g` (global), `i` (case-insensitive), `m` (multiline).

A worked example

Look at the regex this site uses to detect a YouTube video ID in a URL: `[a-zA-Z0-9_-]{11}`. That is a character class containing the 26 letters in both cases, the 10 digits, the underscore, and the hyphen, followed by `{11}` (exactly 11 of the previous element). The whole thing says: "find 11 consecutive characters, each of which is a letter, digit, underscore, or hyphen" — which is what a YouTube video ID looks like.

Common pitfalls

Greedy quantifiers: `*` and `+` match as much as they can, which is rarely what you want. Use `*?` and `+?` (the lazy versions) when you want the shortest match. Catastrophic backtracking: a pattern like `(a+)+b` can take exponential time on input that does not match; modern engines protect against this, but it is worth knowing about. Anchoring: a regex without `^` and `$` will match anywhere in the string, which is sometimes the bug.

Testing and explaining

A regex tester shows the matches inline and explains what each part of the pattern does. Use it before you ship a regex. The free regex tester on this site does both: live match highlighting and a plain-English explanation of every group.

❓ Frequently asked questions

What is a regex?

A regular expression is a pattern that matches text. The pattern is written in a small, dense language. Regexes are built into nearly every programming language, every text editor, and most command-line tools.

Are regexes the same in every language?

Mostly. The core syntax is portable. The differences show up at the edges: lookbehinds, named groups, Unicode property classes, and the specific metacharacters some engines allow or forbid. JavaScript, Python, Perl, and PCRE all use very similar regex dialects; older Unix tools use POSIX ERE or BRE.

When should I not use a regex?

When you are parsing HTML or XML (use a real parser), parsing JSON (use a JSON parser), or doing anything where the input is structured enough that a grammar-based tool is a better fit. Regexes are for patterns, not for grammars.

How do I match an email address?

You don't, in production. A correct email-address regex is hundreds of characters long and still does not cover the full RFC. In practice, "anything that looks like an email followed by an @ followed by anything that looks like a domain" is good enough. RFC 5321 and 5322 are the formal definitions; do not try to implement them in a regex.