Guide

How to Write and Test Regular Expressions

Learn how regex patterns work, how to test them safely, and practical patterns for everyday text tasks.

A regex tester lets you write a regular expression, run it against sample text, and see matches and capture groups highlighted in real time. It is the fastest way to learn, debug, and refine patterns.

Regex basics

A regular expression describes a pattern in text. Literal characters match themselves; metacharacters like ., *, +, ?, [], (), and \ have special meaning. For example, \d matches a digit, \w a word character, . any character, and * means "zero or more of the previous".

Common building blocks

  • \d+ — one or more digits.
  • \w+ — one or more word characters (letters, digits, underscore).
  • [a-z] — any lowercase letter.
  • ^...$ — anchors to the start and end of the string.
  • (... ) — a capture group you can extract.
  • \b — a word boundary.

How to test a regex

  1. Enter your pattern and any flags (g for global, i for case-insensitive, m for multiline).
  2. Paste sample text that should and should not match.
  3. Read the highlighted matches and the list of capture groups.
  4. Refine the pattern and re-test until it matches exactly what you want.

Examples

The pattern \b\w+@\w+\.\w+\b matches simple email-like strings. \d{4}-\d{2}-\d{2} matches dates in YYYY-MM-DD form. ^(Yes|No)$ matches a whole line that is exactly "Yes" or "No". Test each against mixed sample text to confirm it catches the right things and ignores the rest.

When a regex tester is useful

  • Building validation patterns for forms (emails, phones, dates).
  • Debugging a pattern that matches too much or too little.
  • Learning regex with immediate visual feedback.
  • Extracting or replacing patterns in data cleanup.

Put it into practice

Use the Regex Tester right now — free, in your browser, no sign-up required.

Try the Regex Tester