Pattern
Common Patterns
/ / g
Test String
Matches 0 matches
Enter a pattern and test string
Capture Groups
No capture groups

About Regular Expressions

Regular expressions (regex) are patterns used to match character combinations in text. They are supported in virtually every programming language and are essential for search, validation, and text processing tasks.

Syntax Quick Reference

Pattern Description Example
.Any character except newlinea.c matches "abc", "a1c"
\dAny digit (0-9)\d{3} matches "123"
\wWord character (letter, digit, underscore)\w+ matches "hello_1"
\sWhitespace (space, tab, newline)\s+ matches spaces between words
^Start of string (or line in multiline mode)^Hello matches "Hello world"
$End of string (or line in multiline mode)world$ matches "Hello world"
*Zero or more of previousab*c matches "ac", "abc", "abbc"
+One or more of previousab+c matches "abc", "abbc" but not "ac"
?Zero or one of previous (optional)colou?r matches "color" and "colour"
{n,m}Between n and m of previous\d{2,4} matches 2 to 4 digits
[abc]Any character in the set[aeiou] matches any vowel
()Capture group(\d+)-(\d+) captures two numbers
|Alternation (OR)cat|dog matches "cat" or "dog"
\bWord boundary\bword\b matches whole word only

Character Classes

[abc] matches any single character in the set. [^abc] matches any character not in the set. [a-z] matches a range. Shorthand classes like \d (digits), \w (word characters), and \s (whitespace) cover common patterns. Their uppercase versions (\D, \W, \S) match the opposite.

Quantifiers

By default, quantifiers are greedy - they match as much as possible. Adding ? makes them lazy (match as little as possible). {n} matches exactly n times, {n,} matches n or more, and {n,m} matches between n and m times.

Groups and Lookarounds

  • Capturing group (pattern) - extracts matched text, referenced as \1, $1
  • Non-capturing group (?:pattern) - groups without capturing
  • Named group (?<name>pattern) - captures with a name
  • Positive lookahead (?=pattern) - asserts what follows without consuming
  • Negative lookahead (?!pattern) - asserts what does not follow
  • Positive lookbehind (?<=pattern) - asserts what precedes
  • Negative lookbehind (?<!pattern) - asserts what does not precede