[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
('aaa', 'bbb', 'ccc')
>>> line.split() # split data with fixed delimiters
['aaa', 'bbb', 'ccc']

>>> re.split(' +', line) # split on general delimiters
['aaa', 'bbb', 'ccc']
>>> re.findall('[^ ]+', line) # find non-delimiter runs
['aaa', 'bbb', 'ccc']

>>> line = 'aaa...bbb-ccc / ddd.-/e&e*e' # handle generalized text
>>> re.findall('[^ .\-/]+', line)
['aaa', 'bbb', 'ccc', 'ddd', 'e&e*e']

At this point, if you’ve never used pattern syntax in the past your head may very well
be spinning (or have blown off entirely!). Before we go into any further examples, let’s
dig into a few of the details underlying the re module and its patterns.


Using the re Module


The Python re module comes with functions that can search for patterns right away or
make compiled pattern objects for running matches later. Pattern objects (and module
search calls) in turn generate match objects, which contain information about success-
ful matches and matched substrings. For reference, the next few sections describe the
module’s interfaces and some of the operators you can use to code patterns.


Module functions


The top level of the module provides functions for matching, substitution, precompil-
ing, and so on:


compile(pattern [, flags])
Compile a regular expression pattern string into a regular expression pattern ob-
ject, for later matching. See the reference manual or Python Pocket Reference for
the flags argument’s meaning.


match(pattern, string [, flags])
If zero or more characters at the start of string match the pattern string, return a
corresponding match object, or None if no match is found. Roughly like a search
for a pattern that begins with the ^ operator.


search(pattern, string [, flags])
Scan through string for a location matching pattern, and return a corresponding
match object, or None if no match is found.


findall(pattern, string [, flags])
Return a list of strings giving all nonoverlapping matches of pattern in string. If
there are any groups in patterns, returns a list of groups, and a list of tuples if the
pattern has more than one group.


finditer(pattern, string [, flags])
Return iterator over all nonoverlapping matches of pattern in string.


Regular Expression Pattern Matching | 1421
Free download pdf