1

I have the following string:

spf=pass (sender IP is 198.71.245.6) smtp.mailfrom=bounces.em.godaddy.com; domainname.com.au; dkim=pass (signature was verified) header.d=godaddy.com;domainname.com.au; dmarc=pass action=none header.from=godaddy.com;

With the following code:

if "Authentication-Results" in n:
    auth_results = n['Authentication-Results']
    print(auth_results)

    spf = re.match(r"spf=(\w+)", auth_results)
    if spf:
       spf_result = spf.group(1)

    dkim = re.match(r"^.*dkim=(\w+)", auth_results)
    print(dkim)
    if dkim:
        dkim_result = dkim.group(1)

The SPF always matches but the DKIM doesn't:

print(dkim) = None

According to the regex testers online it should: https://regex101.com/r/ZkVg74/1 any ideas why it's not i also tried these:

dkim = re.match(r"dkim=(\w+)", auth_results) dkim = re.match(r"^.*dkim=(\w+)", auth_results, re.MULTILINE)

2
  • 1
    is that string at the top all one string or a few different ones? Commented Oct 8, 2018 at 2:30
  • @HenryWoody, it's one strng Commented Oct 8, 2018 at 2:53

2 Answers 2

1

. does not match a newline character by default. Since the dkim in your test string is on the second line and your regex pattern tries to match any non-newline character from the beginning of the string with ^.*, it would not find dkim on the second line. You should either use the re.DOTALL flag to allow . to match a newline character:

dkim = re.match(r"^.*dkim=(\w+)", auth_results, flags=re.DOTALL)

or remove the unnecessary match from the beginning of the string altogether:

dkim = re.search(r"dkim=(\w+)", auth_results)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, adding the re.DOTALL fixed my issue.
0

First, re.match works from the beginning. so your r"dkim=(\w+)" trial doesn't work.

Second, the . symbol matches characters except the new line character. If you want it to, you should explictly force it using re.S or re.DOTALL flag.

Also, you can use [\s\S] or [\w\W] to match any characters.

Try this:
re.match(r"^[\s\S]*dkim=(\w+)", auth_results).group(1)
or this:
re.match(r"^.*dkim=(\w+)", auth_results, re.DOTALL).group(1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.