Reference
Regular expressions, condensed
The subset that covers most day-to-day matching, with the behaviour that usually surprises people written next to it. Unless stated otherwise the syntax is PCRE-compatible and also valid in JavaScript, Python and Go.
Single characters
| Token | Matches | Note |
|---|---|---|
. | Any character except newline | Add the s flag to include newlines |
\d | A digit, [0-9] | Unicode digits with the u flag in JS |
\w | Word character: letters, digits, underscore | ASCII-only unless Unicode mode is on |
\s | Whitespace: space, tab, newline, CR, form feed | Does not match a non-breaking space |
\b | Word boundary, zero width | Position, not a character |
\A \z | Start / end of the whole string | ^ and $ may be line-based |
[^abc] | Any character except a, b, c | Inside brackets ^ negates |
[a-f0-9] | Range | Ranges are codepoint ranges, not locale aware |
Quantifiers
| Token | Meaning | Greedy example |
|---|---|---|
* | Zero or more | a* matches the empty string |
+ | One or more | a+ needs at least one |
? | Zero or one | colou?r matches both spellings |
{2,5} | Between two and five | \d{2,5} |
{3,} | Three or more | \w{3,} |
{4} | Exactly four | \d{4} for a year |
*? +? | Lazy: as few as possible | <.+?> stops at the first closing bracket |
*+ | Possessive: never backtrack | Not available in JavaScript or Python re |
Groups and references
| Token | Purpose | Example |
|---|---|---|
(...) | Capturing group | (\d{4})-(\d{2}) captures year and month |
(?:...) | Non-capturing group | (?:https?|ftp):// |
(?<name>...) | Named group | (?<year>\d{4}); Python uses (?P<year>...) |
\1 | Backreference to group 1 | (\w)\1 finds doubled letters |
\k<name> | Backreference by name | (?<q>['"]).*?\k<q> |
| | Alternation | Binds loosest: ^cat|dog$ is two patterns |
Lookaround
| Token | Meaning | Example |
|---|---|---|
(?=x) | Followed by x, not consumed | \d+(?= ms) matches 40 in "40 ms" |
(?!x) | Not followed by x | \b(?!admin)\w+ |
(?<=x) | Preceded by x | (?<=\$)\d+ captures the amount only |
(?<!x) | Not preceded by x | Fixed width in most engines |
Flags that change meaning
| Flag | Effect | Trap |
|---|---|---|
i | Case insensitive | Turkish dotless i behaves differently under Unicode rules |
m | ^ and $ match at line breaks | Silently changes old patterns |
s | . also matches newline | Easy to over-match across a whole document |
x | Ignore whitespace, allow comments | Whitespace inside a class is still literal |
g | Find all (JS, Perl) | Stateful with lastIndex on a reused regex |
u | Unicode mode (JS, Python 3 by default) | Changes what \w and case folding cover |
Recipes that are usually wrong
Validating an email address with one regular expression is a losing game:
the grammar permits comments, quoted local parts and domain literals. A
practical check is "exactly one unquoted @, no whitespace,
a dot in the domain" — then send a message to it and see whether it
arrives. ^[^@\s]+@[^@\s]+\.[^@\s]+$ is a deliberately loose
screen, not a validator.
The same applies to URLs, phone numbers and dates. Where a parser exists,
use the parser: new URL(), net/mail,
ipaddress, datetime.fromisoformat. Regular
expressions are excellent at finding shapes in text and bad at deciding
whether something is valid.
Backtracking, briefly
A pattern like (a+)+$ on a long string of a
characters followed by a b explores exponentially many ways to
split the input. This is catastrophic backtracking, and it is a denial of
service vector when the pattern runs on attacker-controlled text. Prefer
possessive quantifiers or atomic groups where supported, anchor the pattern,
cap the input length, and put a timeout around any regex that runs on input
you did not write.
Testing tip: keep a small corpus of strings that must match and strings that must not. A pattern that passes both is a requirement you can regress against; a pattern that only "looks right" is a guess.