Regular Expression Hell?

Steven D. Arnold yami at dementian.com
Tue May 9 02:20:41 CEST 2000


On Mon, 8 May 2000, Akira Kiyomiya wrote:

> Okay, these two regular expression codes are from Python Essential Reference
> book and I am pretty confused about these.
> 
> Could someone dare to explan step by step?
> 
> e_mail = re.compile(r'([a-zA-Z][\w-]*@[\w-]+(?:\.[w-]+)*)')

Find at least one letter followed by zero or more alphanumeric character
or `_' characters (\w), OR a dash; followed by `@'; followed by one or
more \w's again, followed by zero or more groups of \.[\w-]+.  The ?:
creates a group without creating a backreference.  This is the group
referred to by the ending `*'; it's saying match zero of more of the group
that begins with `(?:'.


> URL = re.compile(r'((ftp | http)://[\w-]+(?:\.[\w-]+)*(?:/[\w-]*)*)')

Look for either `ftp' or `http', then `:// ... you can probably figure the
rest out, it's very similar to above.

I'd advise picking up "Mastering Regular Expressions" or one of the perl
compendiums such as the perl black book or the Perl Cookbook.  Even though
the latter two are not about python, many of the ideas and examples in
those books can be used when programming python.


steve





More information about the Python-list mailing list