Regex Match Error while extracting email address

Hello. Can someone help me resolve this error?
I am trying to extract the email address from incoming emails, using Match activity.
Also, can I use a message box right after Match to display the result? Or do I need to use For each loop?

Hi,

Matches activity returns multiple matched string as IEnumerable<Match> type.
So you need to set your variable (Email_address) as IEnumerable<Match> type.
We can easily create this type variable using CTRL+K in the textbox.

Then we can get each addresses using For Each activity for example.

Regards,

1 Like

@Lum If the mailBody variable is supposed to have only one mail address, then you can use the below Expression to get only that Address from the regex you are using :

System.Text.RegularExpressions.Regex.Match(mailBody,"yourRegexExpression").Value

Hi supermanPunch,
Do you mean to use the expression you have provided in the Match activity Pattern after input?

@Lum If you are using this then you won’t be needing the Matches Activity. The Input String is the mailBody which I am assuming and in place of “yourRegexExpression” you can use the regex that you have used in Matches Activity. You can just use the Expression in a Message Box and check if it gives the right output.

This is an abstract of the email content:

Dear Team,

Enquiry type : Careers
Full name : [Michael Jackson]
Contact number : [9912345678]
Email : [jabezjireh@hotmail.com]

I’m trying to extract the address inside the [ ] under Email.

I have an error “Value cannot be null.” Pls refer to attached picture.


What should I do?

@Lum Can you remove the ^ at the beginning and $ at the end in regular expression and check ?
Also post the Regular Expression so that we can check if it is able to extract the Correct data

Still the same error.

@Lum Can you post that regular expression here, We can check it on our side and able to give you a better regex if that doesn’t work

Here u go:
System.Text.RegularExpressions.Regex.Match(mail.body,"([a-zA-Z0-9_-.]+)@([a-zA-Z0-9_-.]+).([a-zA-Z]{2,5})").Value

When I add a dot between mail and body, I get the email address “noreply@abc.com” instead of “jabezjireh@hotmail.com”. How do I extract from “Email : [ ]” field?

@Lum The Regular Expression works for me :sweat_smile: Can you Check if the data in mail.Body is in the Expected format like you have mentioned. You can use a Message Box with mail.Body as it’s value to check the Content in it.

This is the sample email format…
Email address message box result

@Lum This is also another way of getting the data you need. Check it :

System.Text.RegularExpressions.Regex.Match(Id,"(?<=Email : ).*").Value.ToString.Replace("[","").Replace("]","")

It returns blank when I use the following expression:
System.Text.RegularExpressions.Regex.Match(mail.body,"(?<=Email : ).*").Value.ToString.Replace("[","").Replace("]","")

@Lum Ok. Then use this expression :

System.Text.RegularExpressions.Regex.Match(mail.Body,"(?<=Email).*").Value.ToString.Replace(":","").Replace("[","").Replace("]","").Trim

This should definitely work. If it doesn’t just use Message box to check the value of mail.Body. There might be variations

1 Like

Oh yes it works! Thank you so much!

2 Likes

@supermanPunch
I just realised that the regex expression you provided to extract the email address will not work on the following email because the word “email” appeared twice. It returns empty. How to select the second “email” result?
Regex error

@Lum Ohh. This is another post :sweat_smile: , Now I’m not sure if this is related to the other post. If it’s related, I’ll do that first and then I’ll come back here to understand what’s next. How’s that ?

No problem. Thanks a lot.

@supermanPunch
Can you help me with this which I posted yesterday?

“I just realised that the regex expression you provided to extract the email address will not work on the following email because the word “email” appeared twice. It returns empty. How to select the second “email” result which is “jabezjireh@hotmail.com”?”