Regex Tester
Test regular expressions with real-time match highlighting, group capture, and a built-in cheat sheet.
Regular Expressions — How They Work and When to Use Them
A regular expression (regex) is a pattern that describes a set of strings. Instead of checking if a string equals exactly "hello", you can check if it matches a pattern — starts with a capital letter, contains digits, is a valid email format. Regex is one of the most powerful text processing tools available across every programming language.
Anatomy of a regex pattern
The pattern ^[A-Z][a-z]+ \d{4}$ reads: start of string, one uppercase letter, one or more lowercase letters, a space, exactly four digits, end of string. This matches "Alice 1990" but not "alice 1990" or "Alice 99". Each piece has a meaning: ^ and $ are anchors, [A-Z] is a character class, + and {4} are quantifiers.
Flags change how matching works
The g flag finds all matches instead of just the first. The i flag makes matching case-insensitive — hello matches "Hello", "HELLO", and "hElLo". The m flag makes ^ and $ match the start and end of each line instead of the whole string. The s flag makes the dot (.) match newline characters.
The most common regex patterns
Email validation: [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}. URL matching: https?://[^\s]+. Phone numbers: \+?[\d\s\-()]{10,}. ISO dates: \d{4}-\d{2}-\d{2}. These are starting points — real-world validation often requires more nuance. Use the tester above to refine patterns against your actual data.
Frequently asked questions
(\d{4})-(\d{2})-(\d{2}) captures year, month, day separately. Named groups use (?<name>...) syntax: (?<year>\d{4}). The match details panel shows all captured groups.foo(?=bar) matches "foo" only if followed by "bar". Negative lookahead foo(?!bar) matches "foo" NOT followed by "bar". Lookbehind uses (?<=...).\. matches a literal dot (not any character). Special characters that need escaping: . * + ? ^ $ { } [ ] | ( ) \. To match a backslash itself: \\.*, +, ?) match as much as possible. Lazy versions (*?, +?, ??) match as little as possible. Example: given <b>text</b>, <.*> greedy matches the whole string; <.*?> lazy matches just <b>.