Python Regular Expressions
Introduction
http://www.amk.ca/python/howto/regex/regex.html
Reg Exp Syntax
http://docs.python.org/lib/re-syntax.html
- \d Matches any decimal digit; this is equivalent to the class [0-9].
- \D Matches any non-digit character; this is equivalent to the class [^0-9].
- \s Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v].
- \S Matches any non-whitespace character; this is equivalent to the class [^ \t\n\r\f\v].
- \w Matches any alphanumeric character; this is equivalent to the class [a-zA-Z0-9_].
- \W Matches any non-alphanumeric character; this is equivalent to the class [^a-zA-Z0-9_].
Compile for Performance
The sequence
prog = re.compile(pat)
result = prog.match(str)
is equivalent to
result = re.match(pat, str)
but the version using compile() is more efficient when the expression will be used several times in a single program.
Examples
The sequence
import re
s = 'abcdef'
re.match(r'^abcdef$', s) # <_sre.SRE_Match object at 0x2b042001d238>
re.match(r'^abcdef$', s).group() # 'abcdef'