Regex Generator & Tester

Test and generate regular expressions. Includes common patterns for email, URL, phone numbers, passwords, and dates. See matches and captured groups instantly.

Matches standard email format: username@domain.extension. Allows letters, numbers, dots, underscores, and hyphens.

Matches Found

0

Test Lines

3

No matches found

Try adjusting your pattern or test text

What Is a Regular Expression?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Regex is used for text validation, searching, and pattern matching across virtually every programming language. For example, you can use regex to validate email addresses, extract phone numbers, or find and replace text patterns.

Regex Syntax Basics

Basic regex building blocks:

SymbolMeaningExample
.Any single character (except newline)c.t matches cat, cut, cot
*0 or more of preceding characterca*t matches ct, cat, caat
+1 or more of preceding characterca+t matches cat, caat (not ct)
?0 or 1 of preceding characterca?t matches ct, cat (not caat)
^Start of string^hello matches hello at the beginning
$End of stringend$ matches end at the end
[abc]Any one character in brackets[aeiou] matches any vowel
[a-z]Range of characters[a-z] matches any lowercase letter
\\dDigit (0-9)\\d+ matches 123, 456
\\wWord character [a-zA-Z0-9_]\\w+ matches words
\\sWhitespace character\\s matches spaces, tabs
|ORcat|dog matches cat or dog

Worked Example: Email Validation

Pattern: ^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

  • ^ — Start of string
  • [a-zA-Z0-9._%-]+ — One or more letters, digits, dots, underscores, percent, or hyphen (username part)
  • @ — Literal @
  • [a-zA-Z0-9.-]+ — One or more letters, digits, dots, or hyphens (domain)
  • \. — Literal dot (escaped)
  • [a-zA-Z]{2,} — 2 or more letters (TLD like .com, .org)
  • $ — End of string

Matches: john@example.com, user+tag@domain.co.uk
Does not match: invalid.email@, @nodomain.com

Flags and Modifiers

Flags modify how regex behaves:

  • g (global): Find all matches, not just the first. Required to find multiple matches.
  • i (case-insensitive): Match ignores uppercase/lowercase differences.
  • m (multiline): ^ and $ match line beginnings/endings, not just string start/end.
  • s (dotall): . (dot) also matches newline characters.
  • u (unicode): Treat pattern as Unicode sequence.

Capture Groups and Backreferences

Parentheses create capture groups:

Pattern: (\d{3})-(\d{2})-(\d{4})
Text: 123-45-6789
Group 1: 123
Group 2: 45
Group 3: 6789

Use captured groups in replacements: Replace "$2/$3" to reformat.

Common Mistakes

  • Forgetting ^ and $: Without them, pattern matches anywhere. /hello/ matches "say hello world".
  • Not escaping special characters: Use \\ to escape . ? + * [ ] ( ) ^ $ |
  • Greedy vs lazy matching: .* is greedy (matches as much as possible). .*? is lazy (matches as little as possible).
  • Not using raw strings: In some languages, use r"" or // to avoid double-escaping.

Performance Considerations

  • Catastrophic backtracking: Overly complex patterns can be slow. Test performance on large inputs.
  • Anchors improve performance: ^ and $ help regex engines exit early.
  • Be specific: [a-zA-Z] is faster than . (dot).
  • Use character classes: \\d is faster than [0-9].

Tools for Regex

  • Regex101.com: Interactive regex tester with explanations and flags.
  • Regex generators: Tools that build regex from examples.
  • IDE support: Most editors highlight regex syntax and test matches.
  • Language docs: Each language (JavaScript, Python, Java) has specific regex variations.

References

  • MDN Regular Expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
  • Regex101: https://regex101.com/
  • RegexOne Tutorial: https://regexone.com/

Frequently Asked Questions

What does ^ and $ mean in regex?
^ means the start of a string, and $ means the end. So ^hello$ matches only the exact string "hello", not "say hello" or "hello there". Without them, the pattern matches anywhere in the string. Use these anchors for validation where you need exact matches.
What's the difference between . and \d?
. (dot) matches any single character except newline. \d matches only digits (0-9). Similarly, \w matches word characters (letters, digits, underscore), and \s matches whitespace. The case-insensitive versions are \D (non-digits), \W (non-word), and \S (non-whitespace).
How do I make a regex case-insensitive?
Add the 'i' flag at the end: /pattern/i. In JavaScript, use the RegExp constructor: new RegExp(pattern, 'i'). This makes the pattern match both uppercase and lowercase characters. You can combine flags like /pattern/gi for global and case-insensitive matching.
What does ?, *, and + do?
These are quantifiers: ? means 0 or 1 occurrence (optional), * means 0 or more, and + means 1 or more. For example, /colou?r/ matches both 'color' and 'colour'. Use {n,m} for specific ranges, like {2,4} for 2 to 4 occurrences, {3,} for 3 or more.
What are capture groups and why use them?
Capture groups are sections of a regex wrapped in parentheses: (group). They extract specific parts of matched text. For example, /(\d{3})-(\d{4})/ on a phone number captures the area code and number separately. Use $1, $2, etc. to reference captured groups in replacements.
How do I exclude certain patterns?
Use negative lookahead (?!pattern) or negative lookbehind (?<!pattern). For example, /(?!admin)\w+/ matches any word that doesn't start with 'admin'. Character exclusion uses [^abc] to match anything except a, b, or c. Use these cautiously as they can be complex to read.

Related Tools