Development · 2026-05-20
Regular expressions (regex or regexp) are patterns used to match character combinations in text. They are one of the most powerful text processing tools available to developers, and they work across virtually every programming language — JavaScript, Python, Java, PHP, Go and more.
At first glance, regex looks intimidating. A pattern like `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$` seems like gibberish. But every regex is built from a small set of simple building blocks. Once you understand those blocks, you can read and write any pattern.
The simplest regex is just text. The pattern `hello` matches the exact string "hello" wherever it appears. Most characters match themselves literally.
A dot matches any single character except a newline. So `h.t` matches "hat", "hit", "hot" and even "h9t".
Square brackets define a set of characters to match. `[aeiou]` matches any single vowel. `[0-9]` matches any digit. `[A-Za-z]` matches any letter.
A caret inside brackets negates the class: `[^0-9]` matches any character that is NOT a digit.
Quantifiers specify how many times a pattern should repeat:
- `*` — zero or more times
- `+` — one or more times
- `?` — zero or one time (optional)
- `{3}` — exactly 3 times
- `{2,5}` — between 2 and 5 times
So `[0-9]+` matches one or more digits: "1", "42", "12345".
Anchors match positions, not characters:
- `^` — start of string (or line)
- `$` — end of string (or line)
- `\\b` — word boundary
The pattern `^hello$` matches only the exact string "hello" with nothing before or after it.
Parentheses create groups: `(abc)+` matches "abc", "abcabc", etc. The pipe symbol means "or": `cat|dog` matches either "cat" or "dog".
`^[A-Z]{1,2}[0-9][0-9A-Z]?\\s?[0-9][A-Z]{2}$`
This matches patterns like "SW1A 1AA", "M1 1AA", "B33 8TH". Breaking it down: one or two letters, a digit, an optional digit or letter, an optional space, a digit, and two letters.
`[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}`
This matches most email addresses: one or more valid characters, an @ symbol, a domain name with at least one dot, and a top-level domain of two or more letters.
`^\\+?[0-9\\s\\-()]{7,15}$`
This flexibly matches phone numbers in various formats: with or without a country code, with spaces, hyphens or parentheses.
`\\b(0?[1-9]|[12][0-9]|3[01])[/\\-](0?[1-9]|1[0-2])[/\\-](19|20)\\d{2}\\b`
This matches dates in DD/MM/YYYY or DD-MM-YYYY format, with basic validation for day (1–31) and month (1–12) ranges.
1. Forgetting to escape special characters — dots, brackets, parentheses and other regex metacharacters need a backslash to match literally. To match a period, use `\\.` not `.`
2. Greedy vs lazy matching — by default, quantifiers are greedy (they match as much as possible). Adding `?` makes them lazy (match as little as possible). This matters when parsing HTML or extracting quoted strings.
3. Not anchoring patterns — without `^` and `$`, a validation pattern matches anywhere in the string. `[0-9]{3}` matches "123" inside "abc123def". Use `^[0-9]{3}$` to match only a three-digit string.
4. Over-engineering — regex is powerful but not always the best tool. For complex parsing (HTML, JSON, CSV), use a proper parser. Regex works best for pattern matching and simple extraction.
- Build patterns incrementally — start simple and add complexity one piece at a time.
- Use a regex tester — our free Regex Tester lets you write patterns, see matches highlighted in real time, and test against multiple inputs.
- Read patterns aloud — "one or more digits, followed by a dash, followed by one or more letters" is how to mentally parse `[0-9]+-[a-zA-Z]+`.
- Keep a cheat sheet — you do not need to memorise everything. Having a reference for quantifiers and special characters saves time.
Regular expressions are one of those skills that seem difficult until they click, and then you wonder how you ever worked without them. Start with simple patterns and build up — you will be writing complex regex within a week.