supervirus
supervirus

Reputation: 97

Create dictionary from successful regex matches in python

I am very new to regex. I am trying to read a log file line by line.And trying to find patterns like below, they can be anywhere on the line , not just the starting.

18514&[email protected]"qwqw  (need to extract only [email protected])
"emailTo":"[email protected]"
pseId="12121212ffsd"

how can i use re.search to search for all three patterns together ?

Upvotes: 0

Views: 278

Answers (1)

user7419765
user7419765

Reputation:

Here is a solution. I hope, it extracts the e-mail address you want.

I could not create a more accurate regex expression, because you did not provide more information about the logfile. Also I am not sure, if I understand your question right.

import re

for line in open('filename', 'r'):
    firstPattern = re.search(r'email=(.*?)"', line)
    secondPattern = re.search(r'"emailTo":"(.*?)"', line)
    thirdPattern = re.search(r'pseId="(.*?)"', line)

    if firstPattern:
        print(firstPattern.group(1))
    elif secondPattern:
        print(secondPattern.group(1))
    elif thirdPattern:
        print(thirdPattern.group(1))

Upvotes: 2

Related Questions