Help with Regular Expressions

Devan L devlai at gmail.com
Wed Aug 10 04:04:41 EDT 2005


Harlin Seritt wrote:
> I have been looking at the Python re module and have been trying to
> make sense of a simple function that I'd like to do. However, no amount
> of reading or googling has helped me with this. Forgive my
> stone-headedness. I have done this with .NET and Java in the past but
> damn if I can't get it done with Python for some reason. As such I am
> sure it is something even simpler.
>
> I am trying to find some matches and have them put into a list when
> processing is done. I'll use a simple example like email addresses.
>
> My input is the following:
> wordList = ['myname1', 'myname2 at domain.tld', 'myname3 at domain.tld',
> 'myname4 at domain', 'myname5 at domain.tldx']
>
> My regular expression would be something like '\w\@\w\.\w' (I realize
> it could and should be more detailed but that's not the point for now).
>
> I would like to find out how to output the matches for this expression
> of my 'wordList' into a neat list variable. How do I get this done?
>
> Thanks,
>
> Harlin Seritt

You need to enclose the '\w's in parentheses. The re module will only
return it if you enclose it in parentheses. Also, you need to use the
'+' so that \w won't just match the first alphanumeric character, but
will match one or more. You also need to escape the '.' because that's
matches any character. So your regular expression would be more like

r'(\w+)@(\w+)\.(\w+)'

Anyways, you can use a list comprehension and the groups() method of a
match object to build a list of tuples
[re.match(r'(\w+)@(\w+)\.(\w+)', address).groups() for address in
wordList]

On a side note, some of the email addresses in your list don't work.
You should use

wordList = ['myname at domain.tld', 'myname_2 at domain.tld',
'myname at domain.tldx']




More information about the Python-list mailing list