04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

240 CHAPTER 10 ■ BATTERIES INCLUDED<br />

The function re.split splits a string by the occurrences of a pattern. This is similar to the<br />

string method split, except that you allow full regular expressions instead of only a fixed separator<br />

string. For example, with the string method split you could split a string by the occurrences<br />

of the string ', ' but with re.split you can split on any sequence of space characters and commas:<br />

>>> some_text = 'alpha, beta,,,,gamma delta'<br />

>>> re.split('[, ]+', some_text)<br />

['alpha', 'beta', 'gamma', 'delta']<br />

■Note If the pattern contains parentheses, the parenthesized groups are interspersed between the<br />

split substrings.<br />

As you can see from this example, the return value is a list of substrings. The maxsplit<br />

argument indicates the maximum number of splits allowed:<br />

>>> re.split('[, ]+', some_text, maxsplit=2)<br />

['alpha', 'beta', 'gamma delta']<br />

>>> re.split('[, ]+', some_text, maxsplit=1)<br />

['alpha', 'beta,,,,gamma delta']<br />

The function re.findall returns a list of all occurrences of the given pattern. For example,<br />

to find all words in a string, you could do the following:<br />

>>> pat = '[a-zA-Z]+'<br />

>>> text = '"Hm... Err -- are you sure?" he said, sounding insecure.'<br />

>>> re.findall(pat, text)<br />

['Hm', 'Err', 'are', 'you', 'sure', 'he', 'said', 'sounding', 'insecure']<br />

Or, you could find the punctuation:<br />

>>> pat = r'[.?\-",]+'<br />

>>> re.findall(pat, text)<br />

['"', '...', '--', '?"', ',', '.']<br />

Note that the dash (-) has been escaped so Python won’t interpret it as part of a character<br />

range (such as a–z).<br />

The function re.sub is used to substitute the leftmost, nonoverlapping occurrences of a<br />

pattern with a given replacement. Consider the following example:<br />

>>> pat = '{name}'<br />

>>> text = 'Dear {name}...'<br />

>>> re.sub(pat, 'Mr. Gumby', text)<br />

'Dear Mr. Gumby...'<br />

See the section “Using Group Numbers and Functions in Substitutions” later in this chapter<br />

for information on how to use this function more effectively.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!