regular expressions and matches
fredrik at pythonware.com
Thu Mar 30 12:09:08 CEST 2006
> I was having issues trying to get my code working so that I could pass
> the IP addresses and it would return a true or false. When it matches I
> get something that looks like this.
> python ip_valid.py
> IP Address : 126.96.36.199
> <_sre.SRE_Match object at 0xb7de8c80>
> As I am still attempting to learn python I am interested to know how I
> could get the above to return a true or false if it matches or does not
> match the IP address.
you can use the return value right away. the match function/method
returns None if it fails to find a match, and None is treated as a false
value in a "boolean context":
if you get a match instead, you get a match object (SRE_Match),
which is treated as True.
if you really really really want to return True or False, you can do
result = bool(re.match(...))
More information about the Python-list