[Tutor] Re.findall()

Michael Lewis mjolewis at gmail.com
Thu Apr 12 18:06:53 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?
>

Here's the "pattern" portion that I don't understand:

re.findall("[^A-Z]+[A-Z]{3}([a-z])[A-Z]{3}[^A-Z]+"

What is going on here? The ^ means to apply this pattern from what I read.

The first pattern and last pattern are in opposite order, meaning that it
goes from [^A-Z]+[A-Z]{3} to [A-Z]{3}[^A-Z]+ - why does the pattern
flip-flop? The first pattern starts as [^A-Z] but the last pattern end with
[^A-Z]. Why do I need the "+" signs? Also, why does this ([a-z]) need the
parenthesis?

>
> 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.
>

I am familiar with wildcards, but I've mostly used * and nothing else.

>
> 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
>
> mjolewis at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120412/8ea7e27a/attachment.html>


More information about the Tutor mailing list