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

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -