Newbie regex question.

Sean 'Shaleh' Perry shalehperry at attbi.com
Wed Mar 13 15:00:57 EST 2002


On 13-Mar-2002 Omri Schwarz wrote:
> 
> I have a regex = re.compile('yadda (widget1) (widget2) yadda')
> and want to search a string I know to have multiple 
> instances of this regex. I'm a tad confused by
> the RE Howto, so I'd like to ask, how do I properly 
> get a for loop to access the widget1 and widget2 
> strings of each instance?
> 
> for instance in regex.search(string).groups() 
> seems to be the right thing, but isn't working.
> 

>>> import re
>>> s= 'yadda widget1 widget2 yadda'
>>> regex = re.compile('yadda (widget1) (widget2) yadda')
>>> for match in regex.search(s).groups(): print match
... 
widget1
widget2

This works here.  Although I would strongly caution you to do:

re_match = regex.search(s)
if re_match != None ......

just in case the regex fails.




More information about the Python-list mailing list