What is Regular Expression (Regex)?
A regular expression (regex) is a sequence of characters that defines a search pattern. Regex is used for:
- Pattern matching: Find text that matches specific patterns (emails, phone numbers, URLs)
- Validation: Verify input formats (passwords, credit cards, dates)
- Search and replace: Find and modify text in documents or code
- Data extraction: Pull specific information from logs, HTML, or datasets
- Security: Detect malicious patterns, filter input, prevent injection attacks
Used in programming languages (JavaScript, Python, PHP, Java), text editors, command line tools (grep, sed), and security tools.
Regex Cheatsheet
.Any character except newline
\dDigit (0-9)
\wWord character (a-z, A-Z, 0-9, _)
\sWhitespace (space, tab, newline)
^Start of string/line
$End of string/line
*0 or more repetitions
+1 or more repetitions
?0 or 1 (optional)
{n,m}Between n and m repetitions
[abc]Character class (a, b, or c)
(group)Capture group
a|bAlternation (a or b)
\bWord boundary
(?=...)Positive lookahead
(?!...)Negative lookahead