What Is a Regex (Regular Expression)?
A regular expression (regex) is a compact pattern that describes a set of strings. Regex engines scan text to find — and optionally replace — any substring that matches the pattern. They are built into every major programming language and are indispensable for input validation, log parsing, search-and-replace, and data extraction.
The One-Line Definition
A regular expression is a sequence of characters that defines a search pattern. A regex engine tests whether a string (or part of it) matches that pattern.
Core Syntax Reference
| Token | Meaning |
|---|---|
| . | Any character (except newline by default) |
| \d | Any digit — equivalent to [0-9] |
| \w | Word character — [a-zA-Z0-9_] |
| \s | Whitespace — space, tab, newline, etc. |
| [abc] | Character class — matches a, b, or c |
| [^abc] | Negated class — matches anything except a, b, c |
| ^ | Anchor — start of string (or line with m flag) |
| $ | Anchor — end of string (or line with m flag) |
| * | Quantifier — 0 or more (greedy) |
| + | Quantifier — 1 or more (greedy) |
| ? | Quantifier — 0 or 1; also makes * or + lazy when appended |
| {n,m} | Quantifier — between n and m occurrences |
| (abc) | Capturing group — matches abc and captures the result |
| (?:abc) | Non-capturing group — groups without capturing |
| a|b | Alternation — matches a or b |
Practical Examples
^\d{4}-\d{2}-\d{2}$Matches:
2024-01-15[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}Matches:
ada@example.comhttps?://[^\s]+Matches:
https://example.com/path?q=1^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$Matches:
550e8400-e29b-41d4-a716-446655440000Greedy vs Lazy Quantifiers
.*)Matches as much as possible. Given <a>foo</a><b>bar</b>, the pattern <.*> matches the whole string..*?)Matches as little as possible. The pattern <.*?> matches only <a> — stopping at the first >.Test a Regex Now
Test any regex pattern against your input in real time. Open the Regex Tester →
Frequently Asked Questions
What is the difference between greedy and lazy quantifiers?
* and + are greedy — they match as much as possible. *? and +? are lazy — they match as little as possible. Greedy is the default; lazy is safer when parsing HTML or nested structures.
What are regex flags?
Flags modify matching behavior. Common ones: i (case-insensitive), g (global — find all matches), m (multiline — ^ and $ match line start/end), s (dotAll — . matches newlines too).
How do I test a regex online?
Use the Regex Tester on SmartDevBox. Paste your test string and pattern; matches are highlighted in real time with full flag support.