regular expressions and matches
Fredrik Lundh
fredrik at pythonware.com
Thu Mar 30 05:09:08 EST 2006
"Johhny" wrote:
> 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 : 192.158.1.1
> <_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":
http://docs.python.org/ref/Booleans.html
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(...))
</F>
More information about the Python-list
mailing list