search

Home  >  Q&A  >  body text

Regular expression - how to match IP address in python

Now I need to match all the IP addresses in the list that are not x.x.x.1. I don’t know how to do this. What I think of is to use regular matching. I don’t know much about regular matching. Can anyone help me?

阿神阿神2763 days ago934

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-06-28 09:25:37

    Rudimentary version~, it does not match the legality of ip, it only matches x.x.x.1

    import re
    ip_list = [
        '1.2.2.1',
        '1.2.3.3',
        '16.25.22.1',
        '112.23.22.1',
        '11.2.2.1',
        '1.2.32.11',
    ]
    ip_regx = re.compile(r'(?:[\d]+\.)+1$')
    for _ in ip_list:
        if re.search(ip_regx, _):
            print _
    

    reply
    0
  • Cancelreply