Regular Expressions Guide: Learn Regex with Examples 2026
Master regular expressions from basics to advanced patterns. Learn metacharacters, quantifiers, groups, and 10 practical regex patterns for validation and text extraction.
Regular expressions (regex) are one of the most powerful tools in a developer's toolkit. Whether you're validating user input, extracting data from logs, or performing complex text transformations, regex can handle it all. This guide covers everything from basic syntax to practical patterns you'll use daily in 2026.
What Are Regular Expressions?
A regular expression is a sequence of characters that defines a search pattern. Originally developed in the 1950s by mathematician Stephen Kleene, regex has become a standard feature in virtually every programming language. Think of regex as a mini programming language designed specifically for pattern matching.
- Pattern matching: Find text that follows a specific structure
- Validation: Check if input conforms to expected formats (emails, phone numbers, dates)
- Extraction: Pull specific data from larger text blocks
- Replacement: Transform text by finding and replacing patterns
- Splitting: Break text into parts using pattern-based delimiters
Try this tool now:
Try Our Regex Tester →Basic Regex Syntax
Regex syntax can look intimidating at first, but it's built from simple building blocks. Here are the fundamental elements:
Metacharacters
- . (dot) — Matches any single character except newline
- \d — Matches any digit (0-9), equivalent to [0-9]
- \w — Matches any word character (letters, digits, underscore)
- \s — Matches any whitespace (space, tab, newline)
- \b — Word boundary — matches the position between a word and non-word character
- ^ — Matches the start of the string (or line in multiline mode)
- $ — Matches the end of the string (or line in multiline mode)
Quantifiers
- * — Zero or more occurrences: a* matches '', 'a', 'aaa'
- + — One or more occurrences: a+ matches 'a', 'aaa' but not ''
- ? — Zero or one occurrence: colou?r matches 'color' and 'colour'
- {n} — Exactly n occurrences: \d{4} matches exactly 4 digits
- {n,m} — Between n and m occurrences: \d{2,4} matches 2 to 4 digits
- {n,} — n or more occurrences: \d{3,} matches 3 or more digits
Groups and Alternation
- (abc) — Capture group: captures the matched text for later use
- (?:abc) — Non-capture group: groups without capturing
- (?<name>abc) — Named capture group: captures with a label
- a|b — Alternation: matches either 'a' or 'b'
- (?=abc) — Positive lookahead: matches if followed by 'abc'
- (?<=abc) — Positive lookbehind: matches if preceded by 'abc'
10 Practical Regex Patterns
Here are 10 regex patterns every developer should know, along with explanations of how they work:
- Email: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ — Matches standard email addresses
- URL: https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*) — Matches HTTP/HTTPS URLs
- Phone (International): (\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?[-.\s]?\d{3,4}[-.\s]?\d{3,4} — Matches international phone formats
- IPv4 Address: \b(?:\d{1,3}\.){3}\d{1,3}\b — Matches IP addresses like 192.168.1.1
- Date (YYYY-MM-DD): \d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]) — Matches ISO date format
- Strong Password: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$ — Requires uppercase, lowercase, digit, and special character
- Hex Color: ^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$ — Matches #RRGGBB or #RGB format
- HTML Tag: <([a-z]+)([^<]+)*(?:>(.*?)<\/\1>|\s+\/>) — Matches opening and closing HTML tags
- Whitespace Trimming: ^\s+|\s+$ — Matches leading and trailing whitespace
- Korean Characters: [가-힣]+ — Matches one or more Korean Hangul characters
Practical Applications
Regex shines in real-world scenarios. Here are common use cases developers encounter daily:
- Form validation: Verify email, phone, password strength before submission
- Log parsing: Extract timestamps, error codes, and IP addresses from log files
- Data cleaning: Remove unwanted characters, normalize whitespace, strip HTML tags
- URL routing: Match and extract parameters from URL paths in web frameworks
- Code refactoring: Find and replace patterns across large codebases
- Web scraping: Extract structured data from HTML content
Regex Tester Tools Compared
Several online tools help you test regex patterns. QuickFigure's Regex Tester stands out for its simplicity and privacy — all processing happens in your browser with zero data sent to servers. Unlike regex101 which requires an internet connection for its server-side processing, QuickFigure works entirely offline once loaded.
Try this tool now:
Test Your Regex Now →Frequently Asked Questions
What's the difference between * and + in regex?
* matches zero or more occurrences (the preceding element is optional), while + matches one or more (at least one occurrence is required). For example, ab*c matches 'ac', 'abc', 'abbc', but ab+c only matches 'abc', 'abbc' — not 'ac'.
How do I make regex case-insensitive?
Add the 'i' flag to your regex. In JavaScript: /pattern/i or new RegExp('pattern', 'i'). This makes the pattern match both uppercase and lowercase characters.
What are capture groups used for?
Capture groups (parentheses) let you extract specific parts of a match. For example, the pattern (\d{4})-(\d{2})-(\d{2}) on '2026-03-28' captures '2026', '03', and '28' as separate groups that you can reference individually.
Is regex the same across all programming languages?
The core syntax is similar, but each language has its own regex engine with varying feature support. JavaScript, Python, Java, and Go all support PCRE-style regex with minor differences in advanced features like lookbehinds and Unicode support.
How can I avoid catastrophic backtracking?
Catastrophic backtracking occurs when a regex has nested quantifiers that cause exponential time complexity. Avoid patterns like (a+)+ or (a|aa)+. Use atomic groups (?>) or possessive quantifiers a++ where available, and prefer specific character classes over .* when possible.
▶Try the tools from this article
Minjae
Developer & tech writer. Deep dives into dev tools and file conversion technology.
Found this helpful? Get new guide alerts
No spam. Unsubscribe anytime. · By subscribing, you agree to our Privacy Policy.