Home All Tools About Search Tools

Regex Tester

Free online regex tester tool. Test regular expressions with live matching, detailed explanations, and multiple test strings. Supports PCRE, JavaScript, and Python regex flavors.

0 Matches
0 Groups
0ms Processing Time
0 chars

Test Strings

Replacement

Use $1, $2 for captured groups $$ for literal $
🔍

No Results Yet

Enter a regex pattern and test strings, then click "Test Regex"

Pattern Complexity
-
Match Rate
0%
Valid Pattern
-

Regex Cheat Sheet

Basics
. Any character except newline
\w Word character [a-zA-Z0-9_]
\W Non-word character
\d Digit [0-9]
\D Non-digit
\s Whitespace character
\S Non-whitespace character
^ Start of string
$ End of string
\b Word boundary
\B Non-word boundary
Quantifiers
* 0 or more times
+ 1 or more times
? 0 or 1 time
{n} Exactly n times
{n,} n or more times
{n,m} Between n and m times
Character Classes
[abc] Any of a, b, or c
[^abc] Not a, b, or c
[a-z] Lowercase letter a to z
[A-Z] Uppercase letter A to Z
[0-9] Digit 0 to 9
[a-zA-Z] Any letter
Groups & Lookarounds
(abc) Capture group
(?:abc) Non-capturing group
(?=abc) Positive lookahead
(?!abc) Negative lookahead
(?<=abc) Positive lookbehind
(?<!abc) Negative lookbehind
| OR operator
Escapes & Special
\ Escape special character
\t Tab
\n New line
\r Carriage return
\f Form feed
\xXX Hexadecimal character
\uXXXX Unicode character

Mastering Regular Expressions: A Complete Guide

What Are Regular Expressions?

Regular expressions (regex or regexp) are powerful patterns used to match, search, and manipulate text. Originally developed in the 1950s for theoretical computer science, they have become indispensable tools in programming, text processing, and data validation. A regex pattern describes a set of strings without having to list all strings in the set.

Fundamental Regex Concepts

1. Literal Characters

The simplest regex patterns match literal characters exactly:

Pattern: cat
Matches: "cat", "catch", "scatter", "certificate"
Does NOT match: "Cat", "CAT", "ca t"

2. Character Classes

Character classes allow you to match any one of several characters:

[aeiou]    # Any vowel
[0-9]      # Any digit
[a-zA-Z]   # Any letter
[^0-9]     # Any non-digit (^ negates the class)
[abc]      # Either a, b, or c

3. Special Character Classes (Shorthand Classes)

Common character classes have shorthand notations:

Shorthand Meaning Equivalent Example
\d Digit [0-9] "123" → 3 matches
\w Word character [a-zA-Z0-9_] "Hello_123" → 9 matches
\s Whitespace [ \t\n\r\f] "a b\nc" → 2 matches
\D Non-digit [^0-9] "a1b2" → 2 matches
\W Non-word [^a-zA-Z0-9_] "a@b#c" → 2 matches
\S Non-whitespace [^ \t\n\r\f] "a b c" → 3 matches

4. Quantifiers

Quantifiers specify how many times a pattern should match:

a*       # Zero or more 'a's
a+       # One or more 'a's
a?       # Zero or one 'a'
a{3}     # Exactly three 'a's
a{3,}    # Three or more 'a's
a{3,6}   # Between three and six 'a's

Greedy vs Lazy Quantifiers:

Text: "<div>content</div><div>more</div>"

Greedy: <div>.*</div>
Matches: "<div>content</div><div>more</div>" (entire string)

Lazy: <div>.*?</div>
Matches: "<div>content</div>" (first match only)

Advanced Regex Features

1. Groups and Capturing

Parentheses create capturing groups that extract parts of the match:

Pattern: (\d{4})-(\d{2})-(\d{2})
Text: "Date: 2024-12-18"
Groups:
  Full match: "2024-12-18"
  Group 1: "2024" (year)
  Group 2: "12" (month)
  Group 3: "18" (day)

Non-capturing groups use (?:pattern) when you don't need to capture the content:

Pattern: (?:Mr|Ms|Mrs)\. (\w+)
Text: "Mr. Smith and Ms. Johnson"
Matches: "Mr. Smith", "Ms. Johnson"
Captured: "Smith", "Johnson" (titles not captured)

2. Lookarounds (Zero-width Assertions)

Lookarounds check for patterns without including them in the match:

Positive lookahead: X(?=Y)    # X followed by Y
Negative lookahead: X(?!Y)    # X not followed by Y
Positive lookbehind: (?<=Y)X  # X preceded by Y
Negative lookbehind: (?
  

3. Backreferences

Backreferences match the same text as previously matched by a capturing group:

Pattern: (\w+)\s+\1
Matches: "hello hello", "the the", "regex regex"
Explanation: \1 refers to whatever was captured by group 1

Practical Regex Examples

1. Email Validation

Pattern: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Explanation:
  ^                    # Start of string
  [a-zA-Z0-9._%+-]+    # Username (one or more allowed chars)
  @                    # Literal @
  [a-zA-Z0-9.-]+       # Domain name
  \.                   # Literal dot
  [a-zA-Z]{2,}         # TLD (2+ letters)
  $                    # End of string

2. URL Extraction

Pattern: https?://(?:www\.)?[a-zA-Z0-9-]+\.[a-zA-Z]{2,}(?:/\S*)?
Matches: 
  "https://example.com"
  "http://www.example.com/path"
  "https://sub.example.co.uk/page?id=123"

3. Phone Number Matching

Pattern: (?:\+?1[-.]?)?\(?[0-9]{3}\)?[-.]?[0-9]{3}[-.]?[0-9]{4}
Matches various formats:
  "123-456-7890"
  "(123) 456-7890"
  "123.456.7890"
  "+1-123-456-7890"

4. HTML Tag Extraction

Pattern: <([a-z][a-z0-9]*)\b[^>]*>(.*?)</\1>
Explanation:
  <                    # Opening bracket
  ([a-z][a-z0-9]*)     # Tag name (captured)
  \b                   # Word boundary
  [^>]*                # Attributes (non-greedy)
  >                    # Closing bracket
  (.*?)                # Content (lazy capture)
  </\1>                # Closing tag matching opening

Performance Considerations

1. Catastrophic Backtracking

Some regex patterns can cause exponential time complexity:

DANGEROUS: (a+)+b
Text: "aaaaaaaaaaaaaaaaaaaaaaaa!"
Problem: Exponential backtracking as engine tries all combinations

BETTER: a+b
Fixed: Linear time matching

2. Optimization Tips

Regex Flavors and Differences

Flavor Description Common Use Notable Features
PCRE Perl Compatible Regular Expressions PHP, Apache, many tools Recursive patterns, conditional expressions
JavaScript ECMAScript regex Web browsers, Node.js Unicode flag, lookbehind (ES2018)
Python re module Python applications Verbose mode, named groups
.NET System.Text.RegularExpressions C#, VB.NET, F# Balancing groups, right-to-left matching
POSIX Standard Unix regex grep, sed, awk Basic and extended modes

Common Regex Pitfalls and Solutions

âš ī¸ Common Mistakes to Avoid

  1. Not escaping special characters: Use \. to match a literal dot, not .
  2. Over-matching with .*: Use .*? for lazy matching or be more specific
  3. Anchoring issues: Remember ^ and $ match start/end of line (with m flag) or string
  4. Unicode handling: Use u flag for proper Unicode character matching
  5. Line break matching: Use [\s\S] instead of . to match any character including newlines

Testing and Debugging Strategies

  1. Start simple: Test basic patterns first before adding complexity
  2. Use multiple test cases: Include both matching and non-matching strings
  3. Test edge cases: Empty strings, very long strings, special characters
  4. Step through matches: Use tools like this one to see exactly what gets matched
  5. Benchmark performance: Test with realistic data sizes to identify bottlenecks

💡 Pro Tip: Build Regex Incrementally

When creating complex regex patterns, build them step by step. Start with the core pattern that matches your simplest case, then add optional parts, then add validation, and finally add anchors and boundaries. Test at each step to ensure your pattern still works as expected.

Frequently Asked Questions

What is a Regular Expression (Regex)?

A regular expression is a sequence of characters that forms a search pattern. It can be used to check if a string contains the specified search pattern, extract parts of a string, or replace parts of a string. Regex is supported by most programming languages and text processing tools.

What regex flavor does this tool support?

This tool primarily supports JavaScript regex syntax (ECMAScript), which is also compatible with most modern regex engines including PCRE (Perl Compatible Regular Expressions). The tool also provides options for different matching modes like case-insensitive, global, and multiline.

How do I match special characters in regex?

Special characters like . * + ? ^ $ { } [ ] ( ) | \ must be escaped with a backslash (\) to match them literally. For example, to match a literal dot, use \. instead of . which means "any character".

What is the difference between greedy and lazy quantifiers?

Greedy quantifiers (like *, +, ?) match as much as possible, while lazy quantifiers (like *?, +?, ??) match as little as possible. For example, for string "abc123def", regex ".*\d" (greedy) matches "abc123" while ".*?\d" (lazy) matches "abc1".

Can I test regex with multiple test strings?

Yes, you can add multiple test strings separated by newlines. The tool will show matches for each string separately. You can also load sample test strings or import from files to test your regex against various inputs.

Is there a limit to regex pattern or test string length?

The tool can handle reasonably large patterns and test strings. However, for performance reasons, extremely complex regex patterns or very large test strings (over 1MB) may cause browser slowdowns. For production use, always test with realistic data sizes.

Back to all tools
Instant Results
Privacy First
Browser Based
100% Free