Using Regex to grab first name, surname, email and phone

Hi

I am looking for some assistance with Regex in splitting the Name field in an email and outputting the results into two variables. Example of the email is below.

Email Template example:
“name : Jo-anne Smith
email : Jo-anne.Smith@gmail.com
Phone : 0400 123 456
additional-comments : Hi
I need to request a leave form.
Kind regards,
Jo-anne Smith”

I would like to be able to grab the first name ‘Jo-anne’ as the first name variable and ‘Smith’ as the surname variable.

I think I have the first name figured it out (some feedback would be great) but I am unable to figure out the surname field.
Regex for first name:
(?<=name : ).*(?= )

Any assistance would be greatly appreciated.

I found this video somewhat helpful but I still couldn’t apply it to my scenario 2 Lookaheads - YouTube

Thank you to @Tharusha_Jayadeera, @CBlanchard and @aksh1yadav for your help in my previous posts (Extracting field data from an email using Regex - #2 by aksh1yadav).

Kind regards

Steve

1 Like

@Steven_McKeering

  1. First store entire input string into variable of type string and say ‘mailString’ and then follow below expression to fetch required fields.

FirstName: mailString.Substring(mailString.IndexOf("name: ")+"name: “.Length).Split(” ".ToCharArray)(0)

surname: mailString.Substring(mailString.IndexOf("name: ")+"name: “.Length).Split(” ".ToCharArray)(1)

Email: mailString.Substring(mailString.IndexOf("email: ")+"email: ".Length).Split(Environment.NewLine.ToCharArray)(0)

Phone: mailString.Substring(mailString.IndexOf("Phone: ")+"Phone: ".Length).Split(Environment.NewLine.ToCharArray)(0)

5 Likes

I appreciate the reply. I will give this one a try and advise the outcome.

1 Like

Hey @Steven_McKeering

Try below Regex from Link -
(?<=name : )(.+)(?= (.*))

Regards…!!
Aksh

This looks promising :smiley: . I am trying all proposed solutions.

Thank you.

Will advise :slight_smile:

Hey @aksh1yadav

Thank you for your reply.

I tried your regex suggestion:
(?<=name : )(.+)(?= (.*))

It’s a very good match for my needs but I am having no luck applying it in UiPath. Through my attempts to implement (and limited knowledge) I can only get either a single character returned (J) or the entire name (Jo-anne Smith) in the outputs pane.

I think I am having trouble correctly referencing the Group 1 (Jo-anne) and Group 2(Smith) from the regex match.

Here is my workflow - Main.xaml (15.1 KB).

And here is an email example for testing purposes.pdf (46.5 KB)

Your continued assistance is appreciated :blush:

Kind regards

Steve

Hi @Steven_McKeering,
Try this Regex Expression:

(\w{1,}\W\w{4,}) (\w{1,}\w{4,})

The output for this will be:

regex_output(0).Groups(1).ToString and regex_output(0).Groups(2).ToString

As you may see below it’s working :slight_smile:

Regex simulation:

1 Like

Hey @Steven_McKeering

I just added my sample sequence for you. just get idea and apply for rest :slight_smile:
Main (3).xaml (15.4 KB)

Regards…!!
Aksh

1 Like

Hi @lakshman

Thank you for your post.

Your suggestions work! :smiley: but the First name solution needed to be modified. (I removed the second "name: ". This was overriding the First name text. I have used it successfully for the First name and Surname. Below is my modified First name value in my Assign activity.

First Name:
VarFirstNameString.Substring(VarFirstNameString.Indexof(“name:”)+" “.Length).Split(” ".ToCharArray)(0)

I really appreciate your assistance :+1:

Kindest regards

Steve

1 Like

Hi @Pablito

I really appreciate your response :slight_smile:

Your solution was the only one to work for me first time :clap:

Your regex and instructions appeared to work (from what I can tell) because of the matches and groups combination. Unfortunately I need the 'name : ’ anchor in the regex to ensure I collect the right names each time (in production these emails are often forwarded from another person - I know I didn’t mention that in my post :pray:).

Thank you for your assistance, I learnt something from your regex (I am new to regex FYI).

Kind regards

Steve

1 Like

Hi @aksh1yadav

Thanks for your really quick response to my posted workflow :slight_smile:

Unfortunately your amended workflow did not work for me. The surname was blank and first name had the full name. Please let me know if I am missing something.

Thank you again.

Kind regards

Steve

Thank you @Tharusha_Jayadeera and @aksh1yadav for your regex help and suggestions! :sunglasses:

I will be modifying your regex anchor suggestions to match the email and phone fields.

I am confident these will ‘do the job’ as the anchors are steady.

Regex below:
(?<=email : ).(?=\n)
(?<=phone : ).
(?=\n)

Example email below with regex results in bold.

name : Jo-anne Smith
email : Jo-AnneSmith@gmail.com
phone : 0400 123 456
organisation :
additional-comments : Hi
I need a leave form sent to my email address
Thanks
Jo

Or check the Regex101 link: regex101: build, test, and debug regex

1 Like

Hey @Steven_McKeering

It works. i do not know what u have changed :slight_smile:

Regards,!!
Aksh

1 Like

Hey @aksh1yadav

I am not sure what is happening either. Maybe different packages? ??? :upside_down_face:

I download and clicked run.
Here are my results
image

Kind regards

Steve

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