mardi 20 mars 2018

Regex Patterns with Python

I'm trying to make a script/program where I can find matching IP Addresses in two text files:

  • One text file that contains a list of IP addresses (Ex: 1.1.1.1)
  • One text file that contains a list of subnets (1.1.1.0/28)

And I want to use regex and I'm not really sure how to do it.

Example:

def check(fname1, fname2):
    f2 = open(fname2)
    f1 = open(fname1)
    pattern = ('\d{1,3}\.\d{1,3}\.\d{1,3}')

    for line in f1:
        p1 = re.match(pattern, line)
        for item in f2:
            p2 = re.match(pattern, item)

So I'm trying to match the two IP addresses with a pattern (the first 3 octets). Then I want to compare the patterns of the two files but I can't seem to figure it out. I apologize if the explanation was poor.

Aucun commentaire:

Enregistrer un commentaire