Python Regular Expressions: Difference between revisions
Jump to navigation
Jump to search
Created page with '== 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 equival…' |
|||
Line 1: | Line 1: | ||
== Introduction == | == Introduction == | ||
http://www.amk.ca/python/howto/regex/regex.html | http://www.amk.ca/python/howto/regex/regex.html | ||
http://www.tutorialspoint.com/python/python_reg_expressions.htm | |||
good tutorial | |||
== Reg Exp Syntax == | == Reg Exp Syntax == |
Latest revision as of 21:05, 13 September 2013
Introduction
http://www.amk.ca/python/howto/regex/regex.html
http://www.tutorialspoint.com/python/python_reg_expressions.htm good tutorial
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'