Regular Expression Cheat Sheet

Regular expression is very often used in NLP. I usually use it to find well-defined patterns in texts.

There are lots of symbols and rules in regex. Here I tabulate some frequently used symbols which I always need to look up online. Selected from this webpage.

Character Legend
\d one digit
from 0 to 9 [0-9]
\D One character that is not a digit as defined by your engine’s \d
\w “word character” [A-Za-z0-9_]
\W One character that is not a word character as defined by your engine’s \w
 \s “whitespace character”: space, tab, newline, carriage return, vertical tab
{3,} Three or more times
\1 Contents of Group 1
^ Start of string or start of line depending on multiline mode. (But when [^inside brackets], it means “not”)
$ End of string or end of line depending on multiline mode. Many engine-dependent subtleties.
\b Word boundary Most engines: position where one side only is an ASCII letter, digit or underscore
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s