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.

CHAPTER 10 ■ BATTERIES INCLUDED 239<br />

Table 10-8. Some Important Functions in the re Module<br />

Function<br />

compile(pattern[, flags])<br />

search(pattern, string[, flags])<br />

match(pattern, string[, flags])<br />

split(pattern, string[, maxsplit=0])<br />

findall(pattern, string)<br />

sub(pat, repl, string[, count=0])<br />

escape(string)<br />

Description<br />

Creates a pattern object from a string with<br />

a regexp<br />

Searches for pattern in string<br />

Matches pattern at the beginning of string<br />

Splits a string by occurrences of pattern<br />

Returns a list of all occurrences of pattern<br />

in string<br />

Substitutes occurrences of pat in string with repl<br />

Escapes all special regexp characters in string<br />

The function re.compile transforms a regular expression (written as a string) to a pattern<br />

object, which can be used for more efficient matching. If you use regular expressions represented<br />

as strings when you call functions such as search or match, they have to be transformed into<br />

regular expression objects internally anyway. By doing this once, with the compile function,<br />

this step is no longer necessary each time you use the pattern. The pattern objects have the<br />

searching/matching functions as methods, so re.search(pat, string) (where pat is a regexp<br />

written as a string) is equivalent to pat.search(string) (where pat is a pattern object created<br />

with compile). Compiled regexp objects can also be used in the normal re functions.<br />

The function re.search searches a given string to find the first substring, if any, that matches<br />

the given regular expression. If one is found, a MatchObject (evaluating to true) is returned;<br />

otherwise None (evaluating to false) is returned. Due to the nature of the return values, the function<br />

can be used in conditional statements, such as<br />

if re.search(pat, string):<br />

print 'Found it!'<br />

However, if you need more information about the matched substring, you can examine<br />

the returned MatchObject. (More about MatchObjects in the next section.)<br />

The function re.match tries to match a regular expression at the beginning of a given string.<br />

So match('p', 'python') returns true, while match('p', 'www.python.org') returns false. (The<br />

return values are the same as those for search.)<br />

■Note The match function will report a match if the pattern matches the beginning of a string; the<br />

pattern is not required to match the entire string. If you want to do that, you have to add a dollar sign to the<br />

end of your pattern; the dollar sign will match the end of the string and thereby “stretch out” the match.

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

Saved successfully!

Ooh no, something went wrong!