Newbie regex question.
Gustavo Cordova
gcordova at hebmex.com
Wed Mar 13 15:03:15 EST 2002
>
> >>> 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.
>
you can get away with only:
re_match = regex.search(s)
if re_match:
....
The comparison vs. None is not necessary, if you only need
to know the boolean value of True|False, since None is False
(er, bool(None) == False). You know.
Also, comparing vs. None is better done with "var is None"
rather than "var == None".
Salutations.
-gustavo
More information about the Python-list
mailing list