Regular expressions for beginners: the 10 patterns you need
A regular expression, or regex, is a short pattern that describes a kind of text. With one line you can find every phone number in a document or check that a field looks like a date.
Try as you read
Open the regex tester in another tab, paste the sample text below, and type each pattern. Matches are highlighted as you type.
Order 1042 shipped on 2026-10-18 to [email protected].
Order 7 is pending. Call 555-0199 for help.1. Plain text
A pattern of ordinary letters matches those letters. The pattern Order matches both lines starting with Order.
2. Character classes
- \d matches one digit, and \D anything that is not a digit.
- \w matches a letter, digit or underscore.
- \s matches a space, tab or line break.
- [abc] matches one of a, b or c, and [a-z] matches one lowercase letter.
- The dot . matches any single character except a line break.
3. How many: quantifiers
The pattern \d+ matches 1042, 2026, 10, 18, 7, 555 and 0199. The pattern \d{3}-\d{4} matches only 555-0199.
- + means one or more, * means zero or more, and ? means optional.
- {3} means exactly three, and {2,4} means two to four.
4. Anchors
^ matches the start of the text and $ the end. With the multiline flag they match the start and end of each line. The pattern ^Order matches only lines that begin with Order.
5. Groups and alternatives
Brackets group parts of a pattern and capture what they match. The pipe | means "or". The pattern (shipped|pending) matches either word, and the table in the tester shows which one each match captured.
6. A date and an email
The email pattern is a practical shortcut. Real email rules are far more complicated, so use it to find things in text, not to decide whether an address is truly valid.
\d{4}-\d{2}-\d{2} matches 2026-10-18
[\w.+-]+@[\w-]+\.[\w-]+ matches [email protected]7. Escaping
Characters such as . * + ? ( ) [ ] { } | ^ $ and the backslash have special meanings. To match one literally, put a backslash before it. To find a full stop, write \. and not just a dot.
8. Greedy versus lazy
Quantifiers are greedy and take as much as they can. On <b>one</b> and <b>two</b>, the pattern <b>.*</b> matches the whole line as one match. Add a question mark, <b>.*?</b>, and it matches each piece separately.
9. Flags
- g finds every match, not just the first.
- i ignores upper and lower case.
- m lets ^ and $ work on each line.
- s lets the dot match line breaks.
- u switches on full Unicode support, which you need for emoji and many scripts.
10. Replacing with groups
In a replacement, $1 and $2 stand for the captured groups. Searching (\d{4})-(\d{2})-(\d{2}) and replacing with $3/$2/$1 turns 2026-10-18 into 18/10/2026.
Mistakes to avoid
- Forgetting that regex flavours differ between languages. The tester uses JavaScript.
- Nested repeats such as (a+)+ can take forever to fail on long text. The tester stops them after 1.5 seconds.
- Trying to parse nested structures such as HTML with a regex. Use a proper parser for that.