The question of splitting a full name into first, middle, and last came up today in the forums. This is a pretty common question with many possible solutions. I came up one solution which is a CFMX UDF that utilizes regular expressions to match the different possible combinations of individual names.
The following function, splitFullName, will accept a parameter fullName and return a structure containing keys firstName, mi, and lastName. Even if no matches are found, it will always return a structure with these three keys, but they might be blank.
I’ve tested the function with a few hundred names in the following forms:
- First Last
- First MI Last
- Last, First
- Last, First MI
The code also includes a function safeMid which is basically a wrapper for mid() but allows passing zero for the position or length without generating an error.