The re Module
“Some people, when confronted with a problem, think ‘I know, I’ll use regular expressions.’ Now they have two problems.”
The re module provides a set of powerful regular expression facilities, which allows you to quickly check
whether a given string matches a given pattern
(using the match function), or
contains such a pattern (using the
search function). A regular expression is a string pattern written in a compact (and
quite cryptic) syntax.
The match function attempts to match a pattern
against the beginning of the given string, as shown in Example 1-54. If the pattern matches
anything at all (including an empty string, if the pattern allows
that!), match returns a match object. The group method can be used
to find out what matched.
Example 1-54. Using the re Module to Match Strings
File: re-example-1.py
import re
text = "The Attila the Hun Show"
# a single character
m = re.match(".", text)
if m: print repr("."), "=>", repr(m.group(0))
# any string of characters
m = re.match(".*", text)
if m: print repr(".*"), "=>", repr(m.group(0))
# a string of letters (at least one)
m = re.match("\w+", text)
if m: print repr("\w+"), "=>", repr(m.group(0))
# a string of digits
m = re.match("\d+", text)
if m: print repr("\d+"), "=>", repr(m.group(0))
'.' => 'T'
'.*' => 'The Attila the Hun Show'
'\\w+' => 'The'You can use parentheses to mark regions in the pattern. If the
pattern matched, the group method can be used to extract the contents of ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access