Learn to use regular expressions by following RegexTip. From @JohnDCook.

Joined November 2009
Finding words that can be spelled with chemical symbols using a regex. The regex can either run in linear time or exponential time, depending on the regex engine. johndcook.com/blog/2026/10/1…
1
15
2,069
Regex testing tip: include both examples that should match and examples that should not match.
1
10
2,233
For ASCII-only case-insensitive matching, use an ASCII/locale mode if available because Unicode casefolding can be surprising.
8
2,008
Very important to keep in mind: different rules apply inside versus outside character classes.
4
2,366
\B is the negation of a word boundary. For example, /\Bcat\B/ matches 'scathing' (cat inside) but not 'cat' as a standalone word.
1
1
14
2,501
The characters * and ? have different meanings in regular expressions than they do in shells. Confusingly similar, but different.
1
15
2,502
Word boundary \b is a position between \w and \W, not a character. /\bcat\b/ matches 'cat' but not 'concatenate'.
1
10
2,346
What we call 'regular expressions' today includes more general patterns than the original definition from the 1950's.
11
2,298
The escape character \ makes special characters literal. For example, \. matches a period.
1
10
2,324
Regex engines differ on what \s includes (e.g., non-breaking spaces). If exact whitespace matters, specify it explicitly.
1
11
2,402
.* will match as much as it can, but then backtrack if the next atom can also be matched. Backtracking on some engines is slow.
1
8
2,347
Greedy quantifiers (*, +, {m,n}) match as much as possible, then backtrack if needed. Lazy quantifiers (*?, +?, {m,n}?) match as little as possible.
2
15
2,358
Remember: \\ inside a string literal becomes \ in the regex. In Python, raw strings can be cleaner, e.g r'\d+' is easier ro read than '\\d+'.
11
2,363
+ means one or more occurrences. For example, /ab+c/ matches 'abc' or 'abbc' or 'abbbbbbc'.
12
2,361
To include a literal ^ in a character class, don't put it first: /[^^]/ matches any char except '^'.
1
2
11
2,447