Guide · Updated 2026-10-08

Regular Expressions for Beginners, with Examples You Can Test

The handful of regular expression building blocks you need, with tested examples for dates, spelling variants and greedy versus lazy matching.

A regular expression, or regex, is a pattern that describes text. You can use one to find, count or replace pieces of text that follow a shape, such as dates or email addresses. Everything below uses JavaScript regex syntax and can be pasted into the Regex Tester to see the matches highlighted.

The building blocks

PatternMeaningExample
abcThe exact text abcabc matches abc
.Any single characterc.t matches cat and cot
?The previous item is optionalcolou?r matches color and colour
+One or morea+ matches a, aa and aaa
*Zero or moreab*c matches ac and abbc
[0-9]Any one digit[0-9]{4} is four digits in a row
(…)A capture group(\w+)@(\w+) captures name and domain
^ and $Start and end of text^Hi matches only at the start

Example 1: spelling variants

The pattern colou?r with the g flag on the text “color colour colr” finds two matches: color at position 0 and colour at position 6. The u is optional, and “colr” is not matched because the o is required.

Example 2: dates

The pattern [0-9]{4}-[0-9]{2}-[0-9]{2} on “Due 2026-10-08 and 2026-11-03” finds both dates. It looks for four digits, a hyphen, two digits, a hyphen and two more digits. It checks the shape only, so it would also match 2026-99-99.

Example 3: capture groups and replacing

Add brackets to remember parts of a match. The pattern ([0-9]+)-([0-9]+)-([0-9]+) applied to 2026-10-08 captures 2026, 10 and 08. In the Find and Replace tool you can then replace with $3/$2/$1 to get 08/10/2026. Named groups work too: (?<year>[0-9]{4})-(?<m>[0-9]{2}) labels the parts.

Example 4: greedy and lazy matching

By default, a pattern takes as much text as it can. On “<b>bold</b>”, the pattern <.+> matches the whole string in one go. Adding a question mark makes it lazy: <.+?> matches <b> and then </b> as two separate tags. This is the most common surprise for beginners.

Flags

  • g finds every match instead of only the first.
  • i ignores upper and lower case.
  • m lets ^ and $ work on each line.

Mistakes to avoid

  • Checking shape but not meaning. A date pattern does not know that month 99 does not exist.
  • Using a regex for everything. Email addresses and web addresses have many valid forms, so a simple pattern misses some. The Email Extractor uses a practical pattern, not a perfect one.
  • Patterns that run for a very long time on large text, caused by nested repeats.
  • Assuming JavaScript regex behaves like Python, PCRE or grep. Lookbehind and named-group syntax differ.

Frequently asked questions

What does the g flag do? It tells the engine to find all matches, not just the first.

Why does my pattern show an error? The tester reports the engine’s message, such as “Unterminated group” for an unclosed bracket.

Try the tools