Using RegEx condition to validate mail body

Hi, wonderful community,

I need your help :frowning:

I am trying to use mail. body with regex condition to validate my mail to perform my next task. if I get keywords such as ( Accept or Accepted / Reject or Rejected) before the word (From: ) because this validation dependency is on reply mail and I don’t want to take keywords which are in below in the mail body.

the code:
mail.body.ToLower.Contains(Regex.Match(β€œ\w[rejected]+(?=\s+From)”)) or mail.body.ToLower.Contains(Regex.Match(β€œ\w[reject]+(?=\s+From)”))

Thank you in Advance :slight_smile:

Hi @Basit_Xe ,

In your syntax input string is missing, only pattern available. Kindly correct this.

Hope this will help you. Thank you.

Hi @Basit_Xe

In the Regex expression You need to defined the Input value

Refer the Screen Shot
image

Regards
Gokul

Hi @Basit_Xe

It should be like this,

mail.body.ToLower.Contains(Regex.Match(mail.subject,β€œ\w[rejected]+(?=\s+From)”)) or mail.body.ToLower.Contains(Regex.Match(mail.subject,β€œ\w[reject]+(?=\s+From)”))

Note: Assuming mail.subject will get the email subject from the object.

Kindly check/test the given solution and close the topic by marking the solving post as β€˜solution’

Thank you.

Thank you very much

Thank you :grinning: :grinning: :grinning:

Can do it like this -
Regex.Match(mail.body.ToLower.Contains,β€œ\w[rejected]+(?=\s+From)”)) or
Regex.Match(mail.body.ToLower.Contains,β€œ\w[reject]+(?=\s+From)”))

mail body as my input

Hi @Basit_Xe

Syntax incorrect
Regex.Match(mail.body.ToLower.Contains,β€œ\w[reject]+(?=\s+From)”))

Contains will return Boolean. Always check return type.

Use like this

Regex.Match(mail.body.ToLower,β€œ\w[rejected]+(?=\s+from)”)) or
Regex.Match(mail.body.ToLower,β€œ\w[reject]+(?=\s+from)”))

Thank you.

I think you want boolean condition then you can also use System.Text.RegularExpressions.Regex.IsMatch

Hi @Basit_Xe

Still you facing some issue? If so, I can clarify on those. Thank you.

Yeap I want boolean type

still brother, i want it as boolean type

Please try this

image

Thank you

your syntax is wrong. Regex.Match("\w[rejected]+(?=\s+From)") - it will return the IEnum, so directly you cant use for contains function.

Regards,
Arivu

with Contains it’s giving an error

System.Text.RegularExpressions.Regex.IsMatch(mail.Body.ToLower.Contains,β€œ\w[rejected]+(?=\s+From)”) Or System.Text.RegularExpressions.Regex.IsMatch(mail.Body.ToLower.Contains,β€œ\w[reject]+(?=\s+From)”)

or without contains , I will test it now

System.Text.RegularExpressions.Regex.IsMatch(mail.Body.ToLower,β€œ\w[rejected]+(?=\s+From)”) Or System.Text.RegularExpressions.Regex.IsMatch(mail.Body.ToLower,β€œ\w[reject]+(?=\s+From)”)

Hi @Basit_Xe

Please change β€˜From’ to β€˜from’ in pattern to get a match since you are already converting the email content to lowercase.

System.Text.RegularExpressions.Regex.IsMatch(mail.Body.ToLower,β€œ\w[rejected]+(?=\s+from)”) Or System.Text.RegularExpressions.Regex.IsMatch(mail.Body.ToLower,β€œ\w[reject]+(?=\s+from)”)

I already highlighted this in previous post. Thank you.

Use Is Match Activity
Input : mail.Body.ToLower
Pattern : (Accept|Accepted|Reject|Rejected).*(?=\s+From)
OUtput will be true or false

Regards,
Arivu

I want Boolean value for my if condition ( IsRejected / IsAccepted ) these are my variables and I want to use these values to check either mail body contains accept or reject via Regex to take the mail body ( plus this mail body is actually a replay and it contains multiple replies which might have reject as keyword on pervious mails but on latest mails the body contains accept by sender, so I want to use this regex condition to take value above last (From:) in mail body to avoid taking previous replies

Please share a sample email body here as a txt file

Accept

Best Regards
Basit

From: Ahmed11222@gmail.com
Ahmed11222@gmail.com
Sent: Monday, December 12, 2021
To: Basit31313@gmail.com
Basit31313@gmail.com

Dear Basit,

choose either accept or reject to continue the process

Best Regards,
Ahmed

so as you can see in this mail reply there are total 3 keywords(accept, accept. reject) so I run this as normal [ IsAccepted = mail.body.ToLower.Contains(β€œaccept”) ]

[ IsRejected = mail.body.ToLower.Contains(β€œreject”) ]
it will take the reject value