
Learn to use regular expressions by following RegexTip. From @JohnDCook.
Joined November 2009
- Tweets5K
- Following19
- Followers73.8K
- Likes20
Pinned Tweet
Four tips for learning regular expressions
johndcook.com/blog/2008/01/1…
Using regular expressions to search genetic data sciencedirect.com/science/ar…
Outside a character class, ^ means 'start at the beginning of a line'. Example: /^The/ matches lines that begin with 'The'.
\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.
Inside a character class, ^ means 'not'. For example, [^abc] matches any character except a, b, or c.
Searching Greek and Hebrew with regular expressions johndcook.com/blog/2020/09/2…
Use explicit whitespace: /\s+/ matches one or more whitespace characters. /[ \t]+/ restricts it to spaces and tabs.
Inside a character class, period and question mark are not special. For example, [.?] matches a period or a question mark.
Convert a finite automaton to a regular expression: en.wikipedia.org/wiki/Kleene…
Convert a regular expression to a finite automaton: en.wikipedia.org/wiki/Thomps…
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…
Why can't grep find negative numbers? johndcook.com/blog/2019/12/0…
For ASCII-only case-insensitive matching, use an ASCII/locale mode if available because Unicode casefolding can be surprising.
\B is the negation of a word boundary. For example, /\Bcat\B/ matches 'scathing' (cat inside) but not 'cat' as a standalone word.
The characters * and ? have different meanings in regular expressions than they do in shells. Confusingly similar, but different.
Word boundary \b is a position between \w and \W, not a character. /\bcat\b/ matches 'cat' but not 'concatenate'.
What we call 'regular expressions' today includes more general patterns than the original definition from the 1950's.
Four tips for learning regular expressions johndcook.com/blog/2008/01/1…
