freeregextester.app

How to Write Regular Expressions — Free Guide

Writing regex patterns becomes straightforward once you have a systematic approach. Instead of guessing at syntax, you can build patterns piece by piece, test each addition, and arrive at exactly the match you need. This guide walks you through the process from blank pattern to working regex, with practical examples you can test in freeregextester.app.

How do I write a regex pattern from scratch?

Start with the concrete. Look at the strings you want to match and identify the parts that stay the same versus the parts that change. The static parts become literal characters in your pattern. The variable parts become regex syntax.

Here is a step-by-step example. Suppose you want to match phone numbers like 555-123-4567:

  1. The format is three digits, a dash, three digits, a dash, four digits
  2. Write the digit parts: \d{3} for three digits, \d{4} for four
  3. Add the literal dashes: \d{3}-\d{3}-\d{4}
  4. Test it in freeregextester.app with sample phone numbers
Pattern \d{3}-\d{3}-\d{4}

Test String Call 555-123-4567 or 800-555-9012 for info

What are the most common regex patterns?

These patterns cover the vast majority of everyday regex tasks. Bookmark this table or test each one in freeregextester.app to see them in action.

Use Case Pattern Example Match
Numbers \d+ 42, 1000, 7
Email [\w.-]+@[\w.-]+\.\w{2,} user@example.com
URL https?://\S+ https://example.com/path
Whole word \bword\b "word" but not "swordfish"
IP address \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} 192.168.1.1
Date (YYYY-MM-DD) \d{4}-\d{2}-\d{2} 2026-04-09
Hex color #[0-9a-fA-F]{6}\b #C49A2A
Trim whitespace ^\s+|\s+$ Leading/trailing spaces

How do I use capture groups in regex?

Capture groups let you extract specific parts of a match. Wrap any part of your pattern in parentheses, and the regex engine will save that sub-match separately. This is essential for parsing structured text.

Pattern (\d{4})-(\d{2})-(\d{2})

Test String Date: 2026-04-09
Group 1: 2026   Group 2: 04   Group 3: 09

You can also use named capture groups for clarity: (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}). This makes your code more readable when you access the captured values. Test capture groups visually in freeregextester.app to verify they grab the right substrings.

How do I debug regex that isn't matching?

When your regex does not match what you expect, follow this debugging process:

  1. Simplify: Strip your pattern down to the most basic version and verify it matches. Then add pieces back one at a time.
  2. Check escaping: Characters like ., (, ), [, {, *, +, ?, ^, $, and \ have special meaning. If you want a literal match, escape with backslash.
  3. Check quantifiers: * matches zero or more (including nothing). + requires at least one. ? makes the preceding element optional.
  4. Check anchors: If you use ^ and $, make sure your flags include multiline if testing multi-line strings.
  5. Check greedy vs lazy: If your pattern matches too much text, try adding ? after quantifiers to make them lazy.

Using freeregextester.app to debug is the fastest approach. You see matches update in real time as you modify the pattern, so you can isolate exactly where the issue lies.

What is the fastest way to test regex online?

freeregextester.app processes patterns instantly in your browser with zero server roundtrips. Open the app, type your regex, paste your test string, and see highlighted matches immediately. Because it runs locally, there is no latency even on slow connections, and it works offline as a PWA. No sign-up, no ads, no tracking.

Write and test regex now

Build patterns step by step with instant visual feedback in freeregextester.app.

Open Regex Tester

Frequently Asked Questions

How do I start writing a regex pattern from scratch?
Begin by identifying exactly what you want to match. Write down literal examples, then look for the shared structure. Replace variable parts with regex syntax: \d for digits, \w for word characters, [a-z] for lowercase letters. Test each piece incrementally in freeregextester.app.
What are the most useful regex patterns to memorize?
The most commonly used patterns are: \d+ for numbers, [\w.-]+@[\w.-]+\.\w{2,} for emails, https?://\S+ for URLs, \b\w+\b for whole words, and ^\s+|\s+$ for trimming whitespace. These cover the majority of everyday regex tasks.
How do capture groups work in regex?
Capture groups use parentheses to isolate parts of a match for extraction. For example, (\d{4})-(\d{2})-(\d{2}) matches a date like 2026-04-09 and captures the year, month, and day separately. You can reference these groups in replacements or in your code.
Why is my regex not matching what I expect?
Common issues include forgetting to escape special characters like dots and parentheses, using greedy quantifiers when you need lazy ones, and missing anchors like ^ and $. Paste your pattern into freeregextester.app to see exactly what it matches and refine it step by step.
What is the difference between greedy and lazy matching?
Greedy quantifiers (*, +) match as much text as possible. Lazy quantifiers (*?, +?) match as little as possible. For example, given the text <b>bold</b>, the greedy pattern <.*> matches the entire string, while <.*?> matches just <b> and </b> separately.
Can I test regex online without installing anything?
Yes. freeregextester.app runs entirely in your browser with zero installation. Type a pattern, paste test text, and see results instantly. It works offline as a PWA and never sends your data to any server.
by freesuite.app freesuite.app