How to do this in Python...

Skip Montanaro skip at pobox.com
Thu Jan 23 17:40:22 EST 2003


    Mike> match = re.match(pattern, string)
    Mike> if match:
    Mike>   // do something with match object

Verbose or not, this is approximately the correct way to do it.  You should
test against None, and eschew the C++ comments:

    match = re.match(pattern, string)
    if match is not None:
      # do something with match object

In Python, statements are not expressions, so you can't say

    if (match=re.match(...)):
        # do something with match object

Skip





More information about the Python-list mailing list