Match a name to the best fitting email using regex
<p>def email_matcher(emails_file, names_file):
    matches = {}</p>

<pre><code>with open(names_file, 'r') as names:
    for i in names:
        with open(emails_file, 'r') as emails:
            first = i[:(i.index(' '))]
            pattern2 = i[0]
            last = i[::-1].strip()
            last = last[0:(last.index(' '))][::-1]
            for j in emails:
                if re.search(first,j):
                    matches[i] = j
                elif re.search(last,j):
                    matches[i] = j
                else:
                    matches[i] = 'nothing found'
return matches
pass
</code></pre>

<p>This is my code so far, i know it does not work and i get the thing to be no matches found. The goal is to look through all the emails for the best matching email to a name. I have no idea how to make the pattern for regex, i tried looking at the documentation but idk the exact thing to do. What i want to do is check different things in the most accurate order</p>

<p>1 - check if first name last name and middle name are in email
2- check if first name and last name are in email
3 - check if first name last initial
4 - check if first initial last name
5 - check if first name
6 - check if last name</p>

<p>Would it be multiple searches throughout the email with like 6 different regex searches, or is there a way to do one search on every email and see if it hits any of the groups in the pattern</p>

<p>Right now in my code I just have a first name and last name searching that gets none right at all. </p>

<p>Adding emails</p>

<p>Mary Williams - mary.williams@gmail.com</p>

<p>Charles Deanna West - charles.west@yahoo.com</p>

<p>Jacob Jessica Andrews - jandrews@hotmail.com</p>

<p>Javier Daisy Sparks - javier.sparks@gmail.com</p>

<p>Paula A. Graham - graham@gmail.com ( could not find the best matching one, none had paula. there are multiple paulas and grahams in the names list as well)</p>

<p>Jasmine Sherman - jherman@hotmail.com</p>

<p>Matthew Foster - matthew.foster@gmail.com</p>

<p>Ernest Michael Bowman - ernest.bowman@gmail.com</p>

<p>Chad Hernandez - hernandez@gmail.com</p>

<p>So i just looked through all of these and it seems the pattern is firstinitiallastname, firstname.lastname, or lastname@email. The thing is though there are a shit ton of names and even more emails so I dont know the general case. But I feel like it would suffice if i looked for firstname.lastname@email, then firstinitiallastname@email,then middleinitallastname@email, and then the worst case would just be lastname@email?</p>