- What is a regular expression (regex)?
- A regular expression is a sequence of characters that defines a search pattern. Regex is used for string searching, validation, parsing, and text manipulation in virtually every programming language. Patterns match literal characters, character classes, repetition, anchors, and groups.
- What do the regex flags mean?
- g (global): find all matches, not just the first. i (case-insensitive): match regardless of letter case. m (multiline): ^ and $ match start/end of each line. s (dotAll): makes . match newlines too. u (unicode): enables full Unicode matching for emoji and non-Latin characters.
- What is the difference between + and * quantifiers?
- * matches zero or more occurrences. + matches one or more occurrences. \d* matches zero or more digits (can match an empty string); \d+ requires at least one digit. Adding ? makes them lazy/non-greedy: \d+? matches as few digits as possible.
- How do I match a literal dot or other special characters?
- Escape the special character with a backslash: \. matches a literal dot, \* matches a literal asterisk, \( matches a literal parenthesis. Special characters that need escaping: . * + ? ^ $ { } [ ] | ( ) \. In a regex tester, type two backslashes (\\) if your input field interprets single backslashes.
- What are capture groups and how do I use them?
- A capture group is a section of the pattern in parentheses: (\d+). It captures the matched text for extraction. Named groups use (?<name>\d+) syntax. In the matches table, groups appear in the Group column. Groups are essential for extracting specific parts of a match.
- How do I test a regex for email validation?
- Load the Email example or enter the pattern [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}. Note that email validation via regex is notoriously imperfect — the full RFC 5321 spec is extremely complex. For production use, validate by sending a confirmation email rather than relying solely on regex.
- What is the difference between greedy and lazy matching?
- Greedy quantifiers (*, +, ?) match as much text as possible. Lazy quantifiers (*?, +?, ??) match as little as possible. For example, <.+> greedily matches the entire string "<b>bold</b>"; <.+?> lazily matches just "<b>". Use lazy matching when extracting delimited content.