Using a RegEx to match IP addresses in Python

后端 未结 13 2454
不思量自难忘°
不思量自难忘° 2020-12-01 04:58

I\'m trying to make a test for checking whether a sys.argv input matches the RegEx for an IP address...

As a simple test, I have the following...

imp         


        
13条回答
  •  一向
    一向 (楼主)
    2020-12-01 05:23

    """ regex for finding valid ip address """

    import re
    
    
    IPV4 = re.fullmatch('([0-2][0-5]{2}|\d{2}|\d).([0-2][0-5]{2}|\d{2}|\d).([0-2][0-5]{2}|\d{2}|\d).([0-2][0-5]{2}|\d{2}|\d)', '100.1.1.2')
    
    if IPV4:
        print ("Valid IP address")
    
    else:
        print("Invalid IP address")
    

提交回复
热议问题