Regex Basics for Beginners: Learn Regular Expressions Step by Step

A friendly introduction to regular expressions: the symbols that matter most, with small examples you can test right away.

Tryyourselfs Team· · 2 min read Tutorial

A regular expression (regex) is a short pattern that describes text. You can use it to find, check or replace text: for example, to find every phone number in a document or to check that an e-mail field looks right. The syntax looks strange at first, but a handful of symbols cover most needs.

Open the Regex Tester in another tab and try every example as you read.

1. Plain text matches itself

The pattern cat matches the letters "cat" wherever they appear.

2. Character classes

  • . matches any single character.
  • \d matches a 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. [a-z] matches any lower-case letter.

3. Quantifiers: how many times

  • * means zero or more, + means one or more, ? means zero or one.
  • {3} means exactly three, {2,4} means two to four.

So \d{3} matches three digits in a row, and colou?r matches both "color" and "colour".

4. Anchors

  • ^ matches the start of the text and $ the end.
  • \b marks a word boundary.

The pattern ^\d+$ matches text that contains only digits.

5. Groups and alternatives

  • (ab)+ matches "ab", "abab" and so on.
  • cat|dog matches either word.
  • Brackets also let you capture parts of a match so that you can reuse them in a replacement.

6. Escaping

If you want to match a symbol that has a special meaning, put a backslash before it. To match a real full stop, write \..

7. A first real example

A simple pattern for a date such as 2026-11-14:

^\d{4}-\d{2}-\d{2}$

It means: four digits, a dash, two digits, a dash, two digits, and nothing else.

Tips

  • Start simple and build the pattern up one piece at a time.
  • Test against both text that should match and text that should not.
  • Greedy patterns such as .* match as much as possible. Add a ? (.*?) to match as little as possible.
  • Regex is great for simple checks, but very complicated validation, such as perfect e-mail checking, is better done with proper code or a library.

Practise

Use Find and Replace to apply a pattern to a real text, and Text Compare to see exactly what changed.

Regexregular expressionsprogramming

Was this article helpful?

Put it into practice

Try it yourself