Robince Studio
Developer Tools

Regular expressions for beginners, with examples you can test

A regular expression is a compact pattern for finding and replacing text. It looks cryptic at first, but a small set of building blocks covers most everyday jobs. Every example below was tested, and you can paste them into a regex tester to experiment.

3 min read Reviewed Published by Abdul Gani, Robince Studio

On this page
  1. The building blocks
  2. Example 1: a phone-style number
  3. Example 2: capture groups and replacing
  4. Example 3: greedy versus lazy
  5. Example 4: matching emails, with a warning
  6. Lookarounds: matching without consuming
  7. Flags, safety and good habits
  8. Frequently asked questions
  9. Try these tools

The building blocks

PatternMeaningExample match
.Any character except a line breaka.c matches abc
\d \w \sA digit, a word character, a whitespace\d matches 7
[abc] [a-z]One character from a set or range[a-c] matches b
[^abc]Any character not in the set[^0-9] matches x
* + ?0 or more, 1 or more, 0 or 1colou?r matches color and colour
{3} {2,4}Exactly 3, or between 2 and 4 times\d{3} matches 555
^ $Start and end of the text^Hello matches only at the start
|Either this or thatcat|dog
( )A group (captured for reuse)(\d+)-(\d+)

Example 1: a phone-style number

The pattern \d{3}-\d{4} means three digits, a hyphen, then four digits. Run on the text "call 555-1234 now", it matches 555-1234. The braces say how many times the previous item repeats, so you avoid writing \d\d\d.

Example 2: capture groups and replacing

Parentheses capture parts of a match. The pattern (\d{4})-(\d{2})-(\d{2}) run on "on 2026-11-16" captures 2026, 11 and 16 as groups one, two and three. You can use those groups in a replacement to reorder the date: replacing with $3/$2/$1 turns 2026-11-16 into 16/11/2026.

This is how editors and scripts perform bulk reformatting, such as swapping first and last names or converting date formats, without touching each line by hand.

Example 3: greedy versus lazy

Quantifiers are greedy by default: they match as much as they can. Run <.+> on "<b>x</b>" and it matches the entire string <b>x</b>, because .+ grabs everything up to the last >. Add a question mark to make it lazy: <.+?> stops at the first > and matches just <b>.

This difference causes more regex bugs than anything else. If a pattern is matching too much, try the lazy form or a more specific character class such as <[^>]+>.

Do not use regex to parse full HTML. It handles simple cases but fails on nesting and edge cases. Use an HTML parser for real documents.

Example 4: matching emails, with a warning

A practical pattern is ^[\w.+-]+@[\w-]+\.[\w.]+$. It matches asha@example.com. It is good enough to catch obvious typos in a form, but the real rules for email addresses are far more complicated, and no regular expression validates them perfectly.

For important flows, check the format loosely and then verify the address by sending a message. Overly strict patterns reject valid addresses.

Lookarounds: matching without consuming

Lookbehind and lookahead check what is around a position without including it in the match. The pattern (?<=\$)\d+ finds the digits that follow a dollar sign. Run on "cost $45" it matches only 45, not $45. Lookbehind support depends on the engine, so test it in the environment you will use.

Flags, safety and good habits

  • Common flags: g finds all matches, i ignores case, m makes ^ and $ work on each line.
  • Escape special characters with a backslash when you mean them literally, for example \. for a dot and \$ for a dollar sign.
  • Beware of catastrophic backtracking. Patterns with nested repetition such as (a+)+$ can take exponential time on certain inputs and freeze a program. Avoid them on untrusted text.
  • Build patterns step by step and test each stage on sample text, including inputs that should not match.
  • Add a comment or name for any complicated pattern. Regexes are write-easy and read-hard.

Frequently asked questions

What does ^ do inside square brackets?

At the start of a character class it negates it, so [^0-9] matches anything that is not a digit. Elsewhere, ^ outside brackets means the start of the text.

Why does my regex match too much?

Quantifiers like .* and .+ are greedy. Make them lazy with a question mark (.*?) or use a more specific character class.

Are regular expressions the same in every language?

The core is shared, but features such as lookbehind, named groups and Unicode handling vary by engine. Test in the language you will use.

What is a capture group?

Text matched inside parentheses is remembered so you can reuse it in a replacement ($1, $2) or read it in code.

Try these tools

Keep reading