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…
4
666
Regex testing tip: include both examples that should match and examples that should not match.
1
10
2,222
For ASCII-only case-insensitive matching, use an ASCII/locale mode if available because Unicode casefolding can be surprising.
8
2,005
Very important to keep in mind: different rules apply inside versus outside character classes.
4
2,363
\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,478
The characters * and ? have different meanings in regular expressions than they do in shells. Confusingly similar, but different.
1
15
2,500
Word boundary \b is a position between \w and \W, not a character. /\bcat\b/ matches 'cat' but not 'concatenate'.
1
10
2,343
What we call 'regular expressions' today includes more general patterns than the original definition from the 1950's.
11
2,296
The escape character \ makes special characters literal. For example, \. matches a period.
1
10
2,322
Regex engines differ on what \s includes (e.g., non-breaking spaces). If exact whitespace matters, specify it explicitly.
1
11
2,401
.* 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,346
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,357
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,362
+ 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,446
? means a character or expression is optional, i.e. zero or one occurrences. Example: /ab?c/ matches 'ac' and 'abc' but not 'abbc'.
7
2,372
* means zero or more occurrences. Ex: /ab*c/ matches 'ac', 'abc', 'abbc', etc.
11
2,352