Extract Firstname and Last name from the string after specific text format

Hi ,

I am trying to extract Fname and Lname from a string which follows specific text.
This is the sample text

“Begäran om behörighet för p123abc Firstname Lastname - Andra behörighet (random text)”.

I just want first name and last name in two different variables.

Any help would be much appreciated. Thank you .

HI @benjamin.9052

Can you share the Exact output for the query.

Here is extracted data for the last Name

Use Assign Activity

LHS → Create an variable
RHS → Sysytem.Text.RegularExpression.Regex.Match("InputString","(?<=Lastname\s-\s)(\S.+)").Tostring

Regards
Gokul

Here is extracted data for the first Name @benjamin.9052

Use Assign Activity

LHS → Create an variable
RHS → Sysytem.Text.RegularExpression.Regex.Match("InputString",".*(?=Firstname)").Tostring

Regards
Gokul

Hi ,

sorry if i caused a confused a confusion, I am just looking for the string “Firstname” and “lastname” in that statement as my outputs , as that would vary for each case . Just as a sample i replaced the real names with “Firstname” and “lastname” which follows the string P+3 numbers+3chars(P123abc) from the sample.

HI @benjamin.9052

Can you just share the
First Name = ?
Last name = ?

From the below input

Regards
Gokul

“Begäran om behörighet för p123abc Benjamin Kattula - Andra behörighet (random text)”.

So here First name =benjamin , Lastname = Kattula which are the expected outputs from this string

Thank you

working with regex we do require a reliable pattern. have a look here for the start

Hi @benjamin.9052

Use Assign Activity

LHS → Create an variable
RHS → Sysytem.Text.RegularExpression.Regex.Match("InputString","(?<=[a-z]\d{3}\D{3}\s)(\S+)").Tostring

Use Assign Activity

LHS → Create an variable
RHS → Sysytem.Text.RegularExpression.Regex.Match("InputString","(?<=[a-z]\d{3}\D{3}\s)(\S+)\K\s(\S+)").Tostring

Regards
Gokul

1 Like

Thank you , let me try this

we would recommend to cross check your scenario on other names which can be expected:

A simple name like Rob Robot is easy to split on space into firstname Last. But as long we have to expect any names we cannot handle like this. Examples

Double Names:
Ann Mary Johnson - Johnson Last name
Charlotte van Felde, Gregory van den Haag - van Felde, van den Haag Last names

and so on.

Is it reliable to expect only simple firstname lastname pairs?

Thank you , I will keep this in mind . if there are complex names , i will try to change my input parameter and change with a different string .

Thanks Gokul , I had issue with ‘/K’ but I found this helpful.

Great @benjamin.9052

Happy Automation

Regards
Gokul

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.