Learn
What Is a Regular Expression (Regex)?
A regular expression (regex) is a sequence of characters defining a search pattern. Used for validation, extraction, and text transformation in virtually every programming language and text editor.
Basic Syntax
. (any char), * (zero or more), + (one or more), ? (optional), \d (digit), \w (word char), \s (whitespace), [abc] (character class), (group), ^ (start), $ (end).
Common Patterns
Email: [\w.-]+@[\w.-]+\.\w+. URL: https?://[\w.-]+. Phone: \d{3}-\d{3}-\d{4}. Date: \d{4}-\d{2}-\d{2}.
Regex Flags
g (global — all matches), i (case insensitive), m (multiline — ^ and $ match line boundaries), s (dotall — . matches newlines).
Frequently Asked Questions
Are regex the same in every language?
Mostly similar but dialects differ. PCRE, JavaScript, Python, and Java regex engines have subtle differences in features and escaping rules.
What is catastrophic backtracking?
Nested quantifiers like (a+)+ can cause exponential matching time on certain inputs. Use atomic groups or possessive quantifiers to prevent ReDoS attacks.