Regular Expression (@RegexTip) header
Regular Expression (@RegexTip) profile picture

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

Joined November 2009
Using regular expressions to search genetic data sciencedirect.com/science/ar…
1
1,716
Outside a character class, ^ means 'start at the beginning of a line'. Example: /^The/ matches lines that begin with 'The'.
2
10
2,128
\d is not always [0-9]. In Unicode-aware engines, \d may match non-ASCII digits. Use an ASCII mode/flag or [0-9] if you mean only 0-9.
1
2
15
2,689
Inside a character class, ^ means 'not'. For example, [^abc] matches any character except a, b, or c.
1
2
18
2,371
Use explicit whitespace: /\s+/ matches one or more whitespace characters. /[ \t]+/ restricts it to spaces and tabs.
2
14
2,241
Inside a character class, period and question mark are not special. For example, [.?] matches a period or a question mark.
8
2,415
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
16
2,089
Regex testing tip: include both examples that should match and examples that should not match.
1
11
2,247
For ASCII-only case-insensitive matching, use an ASCII/locale mode if available because Unicode casefolding can be surprising.
9
2,011
Very important to keep in mind: different rules apply inside versus outside character classes.
4
2,369
\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,515
The characters * and ? have different meanings in regular expressions than they do in shells. Confusingly similar, but different.
1
15
2,506
Word boundary \b is a position between \w and \W, not a character. /\bcat\b/ matches 'cat' but not 'concatenate'.
1
10
2,350
What we call 'regular expressions' today includes more general patterns than the original definition from the 1950's.
11
2,302
The escape character \ makes special characters literal. For example, \. matches a period.
1
10
2,326