[SOLVED] How do you extract "first name" from "full name" field?

Hello,

I tried my best to find another thread on this topic but I just couldn’t find any. I’m wondering if there is a way to use :truncate on a text field to extract the first part of a name up to the first space. The assumption I’m making is that my users are writing their full name in the form of “first name last name” of course.

I’ve been trying to use the :truncate function and also the extract and replace. But I can’t quite do it. I’m guessing it’s not possible.

But if anyone has succeeded on this, could you please share how you did it exactly so I could try.

Is there a reason they need to enter it in a single field or can you have them do first name field and last name field? Then you could have a full name field that equals those two values. That might be easier!


Gaby | Coaching Bubble

OK! I just found out how to do it.

I must be tired which is why it wasn’t working earlier. I was trying to use the :find and replace function with a regular_expression.

I found one at this stack overflow thread.

  • So when you pull out the Current User’s full name:find and replace

  • press the “use a regexp pattern”

  • insert this in the find: " .*(?:\n|$)" ( Be cautious, there is an important space in the beginning of that regexp )

  • leave the replace empty.

That is it! It works.

10 Likes

I am currently using this, but because I just found a way to use regexp pattern I’d rather not have them do this.

I guess it’s a personal preference thing, but I like having one field of a name rather than separating inputs for first name and last name.

But it is a valid method of course.

Hey, if it works, then great!

This is great; thanks for step-by-step. :slight_smile: I just have a quick question. If I were to use ‘find and replace’ to extract the “last name” as well, from a “full name” field, is this the way to do it?

Within a Text Element:
“Current User’sFullName:find and replace”

  • Leave regex pattern unchecked

In “Text to find” enter:
“Current User’sFullName:find and replace”

  • Check regex pattern
  • Insert " .*(?:\n|$)"
  • Leave the replace empty

-Go back to the original find and replace window
-Leave the replace empty

I did this and it does end up displaying the “last name”, but I wasn’t sure if there was a better way, or a “regex” pattern which would extract the “last name” instead?

2 Likes

I actually just found a simpler method. If anyone here is more knowledgable with Regular Expression, please inform me if I’m using them wrong as I’m a real newbie with RegEx.

So @fayewatson, what you want to do is this:

  • Current User’s full name:find and replace
  • press the “use a regexp pattern”
  • for find: (.*)(\s.*)
  • for replace: $2

This should print out only the part after the space in a full name, so the last name.

Hope it works for you.

7 Likes

Hi @fayewatson,

If you want to understand what the $2 is doing in @ilham.hafizovic 's answer, see here: http://stackoverflow.com/questions/5982824/what-does-1-2-etc-mean-in-regular-expressions .

The gist is that each bracketed set (in above example, this means the bracketed set (.) and (\s.) is capturing text as a group that can later be referenced by a $n where ‘n’ is the number of the bracketed group in the sequence you’re referencing. Meaning that, again, if there are two bracketed groups in ilham’s example, it is the second group you want to reference - the (\s.*) - making the n a 2. So, $2.

This regex feature that Emmanuel enabled is SOOOO powerful in respect of the find and replace.

Hope this helps,
P

6 Likes

Awesome explanation.

1 Like

Thank you so much @scriptschool and @ilham.hafizovic! Those explanations were extremely helpful. Really appreciate it and am looking forward to using this. :slight_smile:

This is great, thanks!

This doesn’t appear to be solved. The “solution” leaves a space in front of the last name, and no first name solution has been presented. (my earlier one had the same problem of leaving a space)

How does one remove the space(s) between first and last name so John Doe becomes JohnDoe

:trimmed

Removes the spaces at the beginning and end of the text. For example, ’ fd ’ becomes ‘fd.’

2 Likes

How to remove the space between the words? Let’s say you have:
Wrong format
“Black Glasses”
Correct format
“BlackGlasses”

Solution + trimmed = the solution for first name.

Then Ilham his other post is the solution for last name:

  • Current User’s full name:find and replace
  • press the “use a regexp pattern”
  • for find: (.)(\s.)
  • for replace: $2

To remove the space between names I haven’t tested that, however I’d assume it would be something like so:

Current User’s full name:find and replace
use a regexp
find: <— put in a space
replace: leave empty

Hope that works for you too @ulrich_00132

2 Likes

None of the expressions on this page or the Stack Overflow link helps what is asked here - extract John from John Doe. I found one that works well. Here it is ^([\w-]+)

2 Likes

Great solution, thanks for posting this

1 Like

Sharing this here hoping to help someone down the line. Here’s the simplest methods.

Get first name? Input User’s Value: extract with regex [Enter: ^\w+ ]
Get the last name? Input User’s Value: extract with regex[Enter: \w+$ ]

FYI. If the user enters 3 Names (first, Mid, Last) - the middle one is ignored.

If you want to capture Middle name on its own use the following: (?:[ ])(\w+)*
First + Middle name: ^\w+ \w+
Middle + Last: \w+ \w+$

No need for the brackets or the space in front of ‘:’ btw. You can either use save in database off of one field or you can use to extract full name from database.

1 Like

To give anyone reading this thread an alternative to regex (this is probably the actual simplest method, but it didn’t exist back when this thread was doing its thing), use Bubble’s :split by operator, split by a space character, and get the first item (or get the last item, if you want the last name).

5 Likes