Extracting and Splitting Name Data in Zapier Using Python

Using Python in Zapier, how can I extract a customer’s ‘Name:’ from an email and separate it into first and last names? Example code:

import re

def split_customer_name(email_content):
    result = re.search(r'Name:\s*([A-Za-z]+\s+[A-Za-z]+)', email_content)
    if result:
        full_name = result.group(1)
        first, last = full_name.split()
        return first, last
    return None, None

email_text = "Event Title: Sample Event\nName: Alice Johnson\nUsername: alicej123\n..."
print(split_customer_name(email_text))

I adjusted a similar script to account for variable name lengths by taking the entire name after ‘Name:’ and then filtering out any non-alphabetic characters. Instead of assuming only two parts, I split the full name and then designated the first token as the given name, while concatenating the remaining tokens into the surname. This allowed better handling of middle names and compound surnames. I also added error logging in case the extraction fails, which helped in troubleshooting email formatting issues in a production environment.

I have tackled similar scenarios where names can be unpredictable. In my experience, using regex to capture only two names doesn’t always work well for clients with middle names or compound last names. One approach was to capture the entire value after ‘Name:’ and then split it into parts. I would then take the first part for the first name and join the rest as the last name. This method accounts for extra segments while still using a simple split logic. It’s important to test with various name formats to ensure reliability.

works well for basic names, but if a customer has more than 2 parts, it might crash. try splitting then joining the extra parts for last name. cheers