Johhny wrote: > Hello, > > I have recently written a small function that will verify that an IP > address is valid.
(snip re.match() based solution - just one remark: compiling the regexp on each function call is more than useless) > 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. If you re-read the fine manual, you'll notice that re.match() returns either None or a Match object. Since, in a boolean context, None evals to False and a Match object to True, just convert the return of re.match to a boolean: return bool(re_ip.match(ipAddress)) BTW, don't mix outputs and a logic. Your validateIp() function should *only* validate, not print anything (except for debugging purpose, but then it should either go to a log or at least to stderr, stdout is for normal program outputs). > I would also like to expand that so that if the > IP is wrong it requests the IP address again and recalls the function. Then wrap the raw_input()/validateIp() into a loop. And wrap this loop into a function !-) def get_valid_ip(prompt="please enter an ip", errormsg="Sorry, %s is not a valid ip"): while True: ip = raw_input("%s : " % prompt).strip() if validateIp(ip): return ip else: print errormsg % ip ip = get_valid_ip() > I have done the same thing in php very easily but python appears to be > getting the better of me. Knowledge doesn't map one-to-one from one language to another. -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list