1

I am parsing a file that have entries like:

xxx-yy.biz.  39405   A   156.154.66.33
mail.global.com.   3464    A   115.113.9.64
xyx xyx xyx
webmail.xyz.com.  1463    A   115.113.9.64
gmail.com.   3464    A   115.113.9.22

I am trying to extact the URL and its IP address with string "mail" in it:

for line in (dnsfile):
            match = re.search(r'(.*mail.*?)\s+(.*)\s+A\s+(.*)', line)

and match.group(1) and match.group(2) is giving me URL and IP.

I want to extent this search so that I don't want to parse public emails like: gmail, hotmail, yahoo,mail. More general : exclude a list of words in this search.

Sanjay Mishra
  • 147
  • 1
  • 1
  • 8
  • Plain regexes can't do this, but a "negative lookahead assertion" might help you. See http://stackoverflow.com/questions/2078915/a-regular-expression-to-exclude-a-word-string and http://stackoverflow.com/questions/1395177/regex-to-exclude-a-specific-string-constant – DrWatson Sep 24 '15 at 21:31

2 Answers2

1

You can use a negative look ahead, but you need to add the start and end anchors so you need re.DOTALL flags too (make the anchors to match from start and end of each line), you can create your negative look-ahead with joining the list of words with | :

re.search(r'^(?!{})(.*mail.*?)\s+(.*)\s+A\s+(.*)$'.format('|'.join(list_of_domin)),line,re.DOTALL)

See demo https://regex101.com/r/bF5xQ3/1

Mazdak
  • 105,000
  • 18
  • 159
  • 188
0

If it's not a requirement to have it as part of the regexp you could do a simple array search

nothanks = ['gmail.com', 'hotmail.com']
for line in (dnsfile):
    match = re.search(r'(.*mail.*?)\.\s+(.*)\s+A\s+(.*)', line)
    if match:
        if not match.group(1) in nothanks:
            print match.group(1)
terbolous
  • 173
  • 5