How to do this in Python...

Paul Rubin phr-n2003b at NOSPAMnightsong.com
Thu Jan 23 18:09:21 EST 2003


"Michael Tiller" <mtiller at ford.com> writes:
> match = re.match(pattern, string)
> if match:
>   // do something with match object
> 
> -or-
> 
> if re.match(pattern, string):
>     match = re.match(pattern, string)
> 
> The first one seems unnecessarily verbose and the second one is inefficient.
> Are there any other options?

Tim Peters suggested one a few days ago:

   if [match for match in [re.match(pattern,string)]][0]:
        # do something with match object




More information about the Python-list mailing list