Regex help to get values from undelivered email

Hi,

I need help with getting the To: email address from the string below.

I know that this expression will get the value. System.Text.RegularExpressions.Regex.Match(CurrentMail.Body.ToString,”(?<=To:).*”).Value.Trim

But before getting the value must check that the value is between “To:” and “Cc:” and only get that value.

From: test@test.com
To: jaira@test.com
Cc: brav@test.com, anjane@test.com

Please let me know how to solve this.
@Yoichi @ppr

Hi @Sairam_RPA ,

Maybe the anchoring could be done by also keeping the Cc in the pattern :

(?<=To:).*\s*(?=Cc:)

image

Hi @Sairam_RPA

FYI another approach

(?<=To:)(.*?)(?=\nCc:)
System.Text.RegularExpressions.Regex.Match(CurrentMail.Body.ToString,"(?<=To:)(.*?)(?=\nCc:)").Value.Trim

Regards!

Sometimes cc could be

CC: or Cc:

How can I handle both here ?

@Sairam_RPA ,

We could Ignore case by adding the RegexOption :

Regex.Match(CurrentMail.Body.ToString,"(?<=To:).*\s*(?=Cc:)",RegexOptions.IgnoreCase).Value.Trim

Ignore case does not work.

The field name is CC: or Cc: . Can we check for those specific values ?

Something like below ?
(?<=To:)(.*?)(?=\nCc: or ?=\nCC: )

how about the following?

(?<=To:).*?(?=(Cc:|CC:|$))

I see the below error message for this

Hi @Sairam_RPA

it is working here, can you try again?
image
image

Regex.Match(strInput, "(?<=To:).*?(?=(Cc:|CC:|$))", RegexOptions.Multiline ).Value

Regards!

1 Like

@Sairam_RPA ,
Try removing the ?=\n from the pattern and check. Also, If you could provide the Input data sample with this variation we can confirm correctly the Regex to be used.

1 Like

The regex works great ! @supermanPunch & @fernando_zuluaga.

Thanks a lot !! :smiley:

1 Like

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