Pinned Tweet
Four tips for learning regular expressions

johndcook.com/blog/2008/01/1…
0
3
8
2,112
\S matches anything but white space, i.e. \S is the opposite of \s. This is a common pattern with upper case vs lower case.
0
0
7
1,606
\s matches white space, i.e. space, tab, newline, etc. Example: /foot\s+ball/ matches 'foot ball' but not 'football'.
0
2
6
2,014
Regular expressions and successive approximation johndcook.com/blog/2022/04/0…
0
1
10
2,192
A common bug: forgetting to escape backslashes in replacement strings in languages that treat backslash as an escape.
1
1
7
1,807
$ anchors a regular expression at the end of a strings. Example: /end$/ matches strings ending in 'end'.
0
0
5
1,968
Portability tip: avoid \R, \h, \v, and other shorthand escapes unless you know your target engines support them.
0
0
4
1,967
In Python, backreferences in replacement use \1 or \g<1> or \g<name>. \g<...> avoids ambiguity like '\11'.
0
1
9
2,355
Using regular expressions to search genetic data sciencedirect.com/science/articl…
0
0
2
1,888
Outside a character class, ^ means 'start at the beginning of a line'. Example: /^The/ matches lines that begin with 'The'.
0
3
11
2,169
\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.
0
1
15
2,758
Inside a character class, ^ means 'not'. For example, [^abc] matches any character except a, b, or c.
1
1
17
2,386
Searching Greek and Hebrew with regular expressions johndcook.com/blog/2020/09/2…
0
0
6
1,629
Use explicit whitespace: /\s+/ matches one or more whitespace characters. /[ \t]+/ restricts it to spaces and tabs.
0
2
14
2,252
Inside a character class, period and question mark are not special. For example, [.?] matches a period or a question mark.
0
0
8
2,422
Convert a finite automaton to a regular expression: en.wikipedia.org/wiki/Kleene%27…
0
1
7
1,693
Convert a regular expression to a finite automaton: en.wikipedia.org/wiki/Thompson%…
1
0
7
2,022
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…
0
1
16
2,096
Regex testing tip: include both examples that should match and examples that should not match.
0
0
11
2,252
Why can't grep find negative numbers? johndcook.com/blog/2019/12/0…
1
0
6
1,959
For ASCII-only case-insensitive matching, use an ASCII/locale mode if available because Unicode casefolding can be surprising.
0
0
9
2,017