Skip to content
devkit.notes

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

TokenMatchesNote
.Any character except newlineAdd the s flag to include newlines
\dA digit, [0-9]Unicode digits with the u flag in JS
\wWord character: letters, digits, underscoreASCII-only unless Unicode mode is on
\sWhitespace: space, tab, newline, CR, form feedDoes not match a non-breaking space
\bWord boundary, zero widthPosition, not a character
\A \zStart / end of the whole string^ and $ may be line-based
[^abc]Any character except a, b, cInside brackets ^ negates
[a-f0-9]RangeRanges are codepoint ranges, not locale aware

Quantifiers

TokenMeaningGreedy example
*Zero or morea* matches the empty string
+One or morea+ needs at least one
?Zero or onecolou?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 backtrackNot available in JavaScript or Python re

Groups and references

TokenPurposeExample
(...)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>...)
\1Backreference to group 1(\w)\1 finds doubled letters
\k<name>Backreference by name(?<q>['"]).*?\k<q>
|AlternationBinds loosest: ^cat|dog$ is two patterns

Lookaround

TokenMeaningExample
(?=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 xFixed width in most engines

Flags that change meaning

FlagEffectTrap
iCase insensitiveTurkish dotless i behaves differently under Unicode rules
m^ and $ match at line breaksSilently changes old patterns
s. also matches newlineEasy to over-match across a whole document
xIgnore whitespace, allow commentsWhitespace inside a class is still literal
gFind all (JS, Perl)Stateful with lastIndex on a reused regex
uUnicode 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.