Loading...
Loading...
Free online regex tester with real-time highlighting — test and debug regular expressions for validation, extraction, and search-and-replace tasks.
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. It is one of the most powerful and widely used tools in text processing — used for validating input formats (email addresses, phone numbers, URLs), extracting data from structured or unstructured text (logs, HTML, CSV), and performing complex search-and-replace operations across strings of any length.
The basic building blocks of regex include literal characters (a, 1, @ match themselves), metacharacters (. matches any character, \d matches digits, \w matches word characters), quantifiers (* for zero or more, + for one or more, ? for zero or one, {n,m} for specific counts), anchors (^ for start of string, $ for end), and groups ((...) for capturing, (?:...) for non-capturing).
Flags modify how the pattern is applied: g (global — find all matches, not just the first), i (case-insensitive matching), m (multiline mode — ^ and $ match line boundaries instead of string boundaries), s (dotall mode — . matches newline characters), and u (unicode — enables \u escapes and correct Unicode property matching). Combine flags as needed: /pattern/gim for global, case-insensitive, multiline.
Capturing groups are one of the most powerful regex features. Parentheses () not only group parts of a pattern but also capture the matched text for later use. For example, (\w+)@(\w+)\.(\w+) captures the username, domain, and TLD separately. These captured values can be referenced as $1, $2, $3 in replacement patterns or accessed programmatically in code.
Performance matters with regex. Poorly written patterns can cause catastrophic backtracking, where the regex engine tries exponentially many ways to match a string. Common causes: nested quantifiers like (a+)+, patterns that match both with and without certain characters, and long strings that almost match. Use atomic groups (?>...) and possessive quantifiers to prevent this. This tool includes performance data to help you test patterns against realistic inputs.
Common practical regex patterns include: ^[\w.-]+@[\w.-]+\.\w{2,}$ for email validation, ^https?://[\w./?=&-]+ for URL matching, ^\+?\d{1,4}[\s-]?\d{6,14}$ for international phone numbers, and ^\d{4}-\d{2}-\d{2}$ for date validation (YYYY-MM-DD). Always test your patterns with both valid and invalid inputs before using them in production code.
By default, quantifiers are greedy — they match as much as possible. Adding ? makes them lazy — they match as little as possible. For example, on the string '<a><b>', the pattern <.+> matches '<a><b>' (greedy, all the way to the last >), while <.+?> matches '<a>' (lazy, stops at the first >).
Lookahead (?=...) asserts that a pattern follows, without consuming characters. Lookbehind (?<=...) asserts that a pattern precedes. For example, (?<=\$)\d+ matches digits preceded by a dollar sign, without including the dollar sign. These are called zero-width assertions.
The most common cause is nested quantifiers like (a+)+ or (.+)*. These create exponential backtracking. Fix by: (1) making patterns more specific, (2) using atomic groups (?>...) to prevent backtracking, or (3) using possessive quantifiers like ++. Test with realistic input lengths to catch performance issues early.
Escape special characters with a backslash. Literal dot is \. (since unescaped . matches any character). Other characters that need escaping: \*, \+, \?, \[, \], \(, \), \{, \}, \^, \$, \|, and \\ itself. Inside character classes [...], most metacharacters lose their special meaning and don't need escaping.
\w matches 'word characters' including letters, digits, and underscore — but what counts as a 'letter' depends on locale and Unicode settings. With the u flag (unicode), \w also matches Unicode letters. The character class [a-zA-Z0-9_] matches only ASCII letters and digits.