Using a RegEx to match IP addresses in Python -


i'm trying make test checking whether sys.argv input matches regex ip address...

as simple test, have following...

import re  pat = re.compile("\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}") test = pat.match(hostip) if test:    print "acceptable ip address" else:    print "unacceptable ip address" 

however when pass random values it, returns "acceptable ip address" in cases, except when have "address" equivalent \d+.

you have modify regex in following way

pat = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") 

that's because . wildcard stands "every character"


Comments

Popular posts from this blog

Glib GIOChannel -

jsp - java.sql.SQLException: [Microsoft][ODBC Microsoft Access > Driver] Too few parameters. Expected 1 -

delphi - When I encode/decode SMS PDU (GSM 7 Bit) user data, do I need prepend the UDH first? -