I recently took on a challenge for myself to come up with a reliable method for extracting the first, middle, and last names from a full name string in Notion. For example, if we have a full name in one database property, can we parse each name from the string and place it into its own property? We can! This layered method works, regardless of how many name segments there are.
We start by creating four formula properties in addition to our “Full Name” text property:
- First Name
- Middle Name
- Last Name
- Last Name (Hidden)
“Last Name (Hidden)”, as it implies, is used strictly to support the other properties, so we can hide it to keep it out of the way. We are going to work backward building our formulas, starting with “Last Name (Hidden)”.
Last Name (Hidden)
This formula will strip out the first name (anything before the first space) from the Full Name string using some simple regex:
replace(prop("Full Name"), "([^ ]+)", "")
Last Name
This formula removes the leading space character from “Last Name (Hidden)”, then removes all characters from before the remaining space:
replaceAll(replace(prop("Last Name (Hidden)"), " ", ""), "(.*\\s)", "")
Middle Name
This formula removes all characters after the last space from “Last Name (Hidden)”. As a result, if the name includes more than three name segments, any additional names in the middle are assigned as middle names. Alternately, if there is no last space that means that there are only two name segments (first and last name), so no middle name will be assigned.
replace(prop("Last Name (Hidden)"), " [^ ]+$", "")
First Name
This formula removes “Last Name (Hidden)” from the “Full Name” property. What we have left is the first name. If there is no value in the Last Name property, the formula simply assigns the value of “Full Name” to the “First Name” property:
if(empty(prop("Last Name")), prop("Full Name"), replace(prop("Full Name"), prop("Last Name (Hidden)"), ""))