Parse IP addresses from txt

后端 未结 1 851
悲哀的现实
悲哀的现实 2021-01-25 08:02

I\'m trying to download a txt file which you can find here. Downloading the file is not a problem:

    testfile = urllib.URLopener()
    testfil         


        
相关标签:
1条回答
  • 2021-01-25 08:05

    You need to remove anchors, since a line won't contain only a single ip-address.

    ip = re.findall( r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", content )
    

    second regex

    r'([0-9]+)(?:\.[0-9]+){3}' 
    

    must return three digit number because only the first three digits are captured, re.findall method would return captures first if there any. If there are no captures, then only it would return the matches. By turning the capturing group into non-capturing group will give you the desired output.

    r'\b[0-9]+(?:\.[0-9]+){3}\b' 
    
    0 讨论(0)
提交回复
热议问题