
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…
\w matches any 'word' character, i.e. letters, numbers, and underscore. Equivalent to [a-zA-Z0-9_]
8
2,356
\w isn't just [A-Za-z0-9_]. In Unicode mode it includes many letters/digits. Use an ASCII flag if you need strict ASCII.
1
8
2,098
How to use regular expressions in Pandas kanoki.org/2019/11/12/how-to…
4
1,697
Matching dates is harder than it looks: leap years, month lengths, etc. Keep 'date-ish' regexes honest about what they validate.
10
2,014
grep Flags – The Good Stuff zwischenzugs.com/2022/02/02/…
Perl's special vars: $& (match), $` (pre-match), $' (post-match). Handy but can slow down regex in some Perl versions.
\d matches any digit. \D matches anything except a digit. 'Digit' can include more characters than you may expect when using Unicode.
7
1,990
\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.
\s matches white space, i.e. space, tab, newline, etc. Example: /foot\s+ball/ matches 'foot ball' but not 'football'.
Regular expressions and successive approximation johndcook.com/blog/2022/04/0…
A common bug: forgetting to escape backslashes in replacement strings in languages that treat backslash as an escape.
$ anchors a regular expression at the end of a strings. Example: /end$/ matches strings ending in 'end'.
5
1,978
Portability tip: avoid \R, \h, \v, and other shorthand escapes unless you know your target engines support them.
4
1,979
In Python, backreferences in replacement use \1 or \g<1> or \g<name>. \g<...> avoids ambiguity like '\11'.
Using regular expressions to search genetic data sciencedirect.com/science/ar…
2
1,898
Outside a character class, ^ means 'start at the beginning of a line'. Example: /^The/ matches lines that begin with 'The'.