Get First and Last name from Full name (example Instagram)

Ok here is a simple guide on how to extract first and last name from full name.

A real-world example use case is when you use Instagram signup/login for your bubble app signup/login.
When creating an account in instagram there is an input to enter full name and not separate first and last name.
image

So if your bubble app you need to have a separate first and last name, and a user signs up with Instagram you will have to extract this from the full name.

So here is one way of doing this:

  1. Note that once you use the call the Sign/login with Instagram action,
  2. you will be able to call the Current User’s Instagram’s Full name action.
  3. Then call the find & replace action.
  4. In the find and replace window check Use a regex pattern.
  5. Use the following regex pattern. Will explain why it looks a bit complex below.
    ([a-zA-Z0-9-_']+)\s*([a-zA-Z0-9-_']*\s*[a-zA-Z0-9-_']*)
  6. To get the first name enter $1 in the replace by box.
    If you want to get the last name use $2.

Note this can work generally to get first and last name from full name.
Only used Instagram here as real use case.

Now you might ask why the regex pattern looks that complicated.
I could have simply used something like this (\w+)\s*(\w+) and you may see this pattern on some online forums.
Problem with using this simple version is that it assume there is always going to be two names in the fullname (e.g. John Doe).
If its a single string it will break (e.g. John).
If there is a middle name it will break (e.g. John James Doe).
If there is a hyphen it will break (John James-Doe).
If there in an apostrophe it will break (e.g. D’Angelo Doe).

The regex i provide in the screenshot will work for all the above cases.
If you look closer you will notice i have underscore, hyphen, apostrophe in the string pattern.
If you expect some other non alpha-numeric characters in the name you can include them.
If you use \w it only matches alpha-numeric characters including underscore but wont match hyphen and apostrophe.

Note that if a full name has three strings like, John James Doe, it will extract John as the first name,
and James Doe as the last name.

Enjoy
Seanhoots

14 Likes

Nice! Very nifty concept!

1 Like

Indeed! More tips here and fewer zombie thread activations, please! :wink:

Super useful. I love it. Thanks for taking the time to share!