How to select only specific data from the body of mail

How to select only specific data from the body of mail i was using
item.body.Split({"User corp IDs (semicolon separated): " , "New approver corp ID: corp"}, StringSplitOptions.None)

but what the problem is it was taking entire body next to User corp IDs (semicolon separated): but i need just name after that attaching how the body of the email looks

New Text Document (2).txt (656 Bytes)

Hi @bpt.teja1996

You can use Regex to extract data from mail body

Sample (Regex for get Frist name and last name):
Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,"(?<=User First and Last names [(]semicolon separated[)]:)[\s\S]*").ToString, "\n.*", "").ToString,"\s","").tostring

Check output
image

For guidelines, You can refer as below:
Extract_Name_Email.zip (17.3 KB)

I hope it will be useful to you :blush:

1 Like

hai @Natapong when i pasted that expression it was showing that compailer error

What error are you facing ?

Could you please share the error message or debug screen.

@Natapong iam trying for user cropIDs line but it was showing error i was trying to assign to strUsername variable strUsername was taken as data type string and can you give me what are the chenges should be done

Please try to add [System.Text.RegularExpressions] namespaces to your project.

1 Like

thankyou so much @Natapong it was working perfect

But i was stuck to write for New approver corp ID i need the word after New approver corp ID: corp\ can you give for that @Natapong it would help me lot please

Hi @bpt.teja1996

Sorry, I found some miss in my code (I forgot to add ^ before \s )

^\s is mean first space
\s is mean all space

You can use below:

New approver corp ID:
NewApproverCorpID = Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,"(?<=New approver corp ID[:]:)[\s\S]*").ToString, "\n.*", "").ToString,"\s","").tostring

Get New approver corp ID value after \
NewApproverCorpID = NewApproverCorpID.Split("\".ToArray)(1).ToString

image

Check output

=========================================================

User First and Last names (semicolon separated):
Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,"(?<=User First and Last names [(]semicolon separated[)]:)[\s\S]*").ToString, "\n.*", "").ToString,"^\s","").tostring

New approver First and Last name:
Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,"(?<=New approver First and Last name:)[\s\S]*").ToString, "\n.*", "").ToString,"^\s","").tostring

Contract Manager first and last name:
Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,"(?<=Contract Manager first and last name:)[\s\S]*").ToString, "\n.*", "").ToString,"^\s","").tostring

Choose new costcenter:
Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,β€œ(?<=Choose new costcenter:)[\s\S]").ToString, "\n.”, β€œβ€).ToString,β€œ^\s”,β€œβ€).tostring

Hi @Natapong with the help of that expressions i have assigned each value to each variable now i want to past them in to an excel can you please help me how to do that

Hi @Natapong it was working only for user name when not working for others i kept message boxes to see but for user name it was showing but foe costcenter crop id i t was not showing please help me @Natapong please
i was attaching my project please help me EmailAutomation.xaml (12.1 KB)

@Natapong hai i want to get only all the mails from only complete yesterday with subject send by display name HRChanges can you help me to to get that please

Hi @bpt.teja1996

I created new regex for extract data from your email.
I changed the condition from deleting all text from the new line to delete all text(including specifying message) after found a specific message.

for details please see as below:

User corp IDs (semicolon separated):
Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,"(?<=User corp IDs [(]semicolon separated[)]:)[\s\S]*").ToString, "(?=User First and Last names [(]semicolon separated[)]:)[\s\S]*", "").ToString,"^\s","").tostring

New approver corp ID:
Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,"(?<=New approver corp ID:)[\s\S]*").ToString, "(?=New approver First and Last name:)[\s\S]*", "").ToString,"^\s","").tostring.Split("\".ToArray)(1).ToString

Choose new costcenter:
Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,"(?<=Choose new costcenter:)[\s\S]*").ToString, "(?=New location:)[\s\S]*", "").ToString,"^\s","").tostring

Requestor Name:
Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,"(?<=Requestor Name:)[\s\S]*").ToString, "(?=Requestor mail ID:)[\s\S]*", "").ToString,"^\s","").tostring

Date of change:
Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,"(?<=Date of change:)[\s\S]*").ToString, "(?=Selected option: User management)[\s\S]*", "").ToString,"^\s","").tostring

=================================================

Condition for email from yesterday
Please try to set Mail Filter by below condition: β€œ[ReceivedTime]>='"+now.AddDays(-1).ToShortDateString+"'"+"AND [ReceivedTime] <= '"+now.ToShortDateString+"'"

And try to put [if] in for each mail list and set below condition to find mail subject β€œHR Change request for Clarity”

If Condition:
item.Subject.Contains(β€œHR Change request for Clarity”)

I hope this information will be useful for you :blush:

@Natapong i have a hyperlink with number INCE112330037(changes numbers for each mail) at the top of the email i need to save that number also how to achieve that

@Natapong i need urgent help

i have a regex expression to select specific data from the email body

Choose new costcenter: BXv GR
Choose new costcenter: Project Cloud computings

The above are example types

in the mail body we will get the data like that with other unwanted stuff . i need to extract value
after the

Choose new costcenter:

and i was storing in a variable

the regex i was using is

Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,β€œ(?<=Choose new costcenter:)[\s\S]").ToString, "\n.”, β€œβ€).ToString,β€œ\s”,β€œβ€).tostring

But it was taking the value after the Choose new costcenter: as BXvGR , ProjectCloudcomputings there is no space between as in the mail body can you suggest how to get space also that

Hi @bpt.teja1996

Please try as below regex:

Regex.Replace(Regex.Replace(System.Text.RegularExpressions.Regex.Match(item.body.ToString,β€œ(?<=Choose new costcenter:)[\s\S]").ToString, "(?=New location:)[\s\S]”, β€œβ€).ToString,β€œ^\s”,β€œβ€).tostring

@Natapong hai i have assigned some value to the variable now i want to check weather the value in the variable is in the format of P0123654 the starting P0 is fixed but the values after that will be varied but totally it would be of 8 digits how to write the if condition to check if the value in the variable is in the format of P0xxxxxx

it should check it should have 8 letters and starting should be P0

Hi @bpt.teja1996

Please try to use condition
YourVariable.Length = 8 and YourVariable like "P0*"


Sample:
CheckIF_variableFormat.zip (21.8 KB)

Thank you so much @Natapong

1 Like