Python Regular Expressions

From Wiki
Revision as of 22:24, 1 February 2011 by Scott (talk | contribs) (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…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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'