Skip to main content

Regex Tester

Test and debug regular expressions in real-time

Enter your regex pattern

Enter a pattern and test string, then click "Test Regex"

About Regular Expressions (Regex)

What are Regular Expressions?

Regular expressions (regex) are powerful patterns used to match, search, and manipulate text. They provide a concise and flexible means for identifying strings of text, such as particular characters, words, or patterns of characters.

  • Pattern matching: Define complex search patterns with simple syntax
  • Text validation: Verify formats like email addresses, phone numbers, URLs
  • Text extraction: Extract specific parts from strings
  • Text replacement: Find and replace text based on patterns
  • Language agnostic: Supported in most programming languages

Common Regex Patterns

Pattern Description Example
. Matches any single character a.c matches "abc", "a1c"
\d Matches any digit (0-9) \d\d matches "12", "45"
\w Matches word characters (a-z, A-Z, 0-9, _) \w+ matches "hello", "test123"
^ Start of string ^Hello matches "Hello" at start
$ End of string world$ matches "world" at end
* Zero or more occurrences a* matches "", "a", "aa"
+ One or more occurrences a+ matches "a", "aa"
? Zero or one occurrence a? matches "", "a"
[] Character class [aeiou] matches any vowel
| Alternation (OR) cat|dog matches "cat" or "dog"

Common Use Cases

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

Matches: user@example.com, test.email+tag@domain.co.uk

Pattern: ^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$

Matches: (123) 456-7890, 123-456-7890, 123.456.7890

Pattern: ^https?://[^\s/$.?#].[^\s]*$

Matches: http://example.com, https://www.example.com/path

Pattern: \d+

Matches: All sequences of digits in "Price: $123.45" → "123", "45"

Regex Flags

  • Case Insensitive (i): Makes the pattern match regardless of case
  • Multiline (m): Makes ^ and $ match the start/end of each line, not just the string
  • Dotall (s): Makes . match newline characters as well
  • Verbose (x): Allows whitespace and comments in the pattern for readability

Best Practices

  • Test thoroughly: Test your regex with various inputs, including edge cases
  • Use anchors: Use ^ and $ when you want to match the entire string
  • Avoid catastrophic backtracking: Be careful with nested quantifiers
  • Escape special characters: Use \ to escape special regex characters when matching literally
  • Use character classes: Use \d instead of [0-9] for better readability
  • Comment complex patterns: Add comments to explain complex regex patterns