[Tutor] Re.findall()

Steven D'Aprano steve at pearwood.info
Thu Apr 12 16:47:30 CEST 2012


mjolewis at gmail.com wrote:
> Hi everyone,
> 
> I am having trouble understanding re.findall(). I've read through the
> documentation and looked at at some examples online, but I still don't have
> a clear picture.
> 
> I am going through pythonchallenge.com and I am on challenge 3. I've see.
> The answer to the problem, but I don't understand the "pattern" portion of
> re.findall().


What part don't you understand? Do you understand what a so-called regular 
expression is?

Regular expressions are like super-charged wildcards. In the DOS or Windows 
command.com or cmd.exe shell, you can use wildcards * and ? to match any 
characters, or a single character. In Linux and Macintosh shells, you have the 
same thing only even more so.

Regular expressions are a mini programming language for wildcards. For 
example, 'a.*z' is a pattern that matches any string starting with the letter 
'a' and ending with the letter 'z'.

Here's a more complicated example:

'm[aeiou]{1,2}n'

This regular expression pattern, or regex, matches the letter 'm', followed by 
1 or 2 vowels ('a', 'e', 'i', 'o', or 'u') in a row, followed by 'n'. So it 
will match "moon" or "mean" or "moan" or "man", but not "mpg" or "marvelous" 
or "meeeeen".

You can learn more about regexes here:

http://docs.python.org/library/re.html
http://docs.python.org/howto/regex.html


-- 
Steven



More information about the Tutor mailing list