Error using replace regular expression

Hi,

My code is:

system.Text.RegularExpressions.Regex.Replace(mail.Headers(“Message-ID”).tostring,@“[/?%*:”<>.]“,”")

What I am trying to do is remove the set of characters from the message ID header and save that to a string. However, I am getting the compile error “Character is not valid”.

Thanks!

@SoqedHozi

Use this syntax mail.Headers(“UID”) at the place of mail.Headers(“Message-ID”)

Cheers

I don’t see UID in my mail headers? Does it matter that I’m using outlook?

Using UID the error changes to “expression expected”.

Is the upper syntax is same as you write in your code.

Because it might show an validation error

Or if it possible can share a workflow where you written this code

It’s just an assign activity. The full value is:

system.Text.RegularExpressions.Regex.Replace(mail.Headers(“UID”).tostring,@“[/?%*:”<>.]“,”“)+”.msg"

I notice that the \ is not showing in the code above, it follows the /

Is this working fine now

No I still have that error as mentioned.

what basically you want to do?

This is so I can use the Message-ID as a unique filename that I can recalculate accurately on demand later in my process. This is because I need to upload the saved mail message.

I have tried it also with just 1 character inside the square brackets, and still returns “expression expected”.

Hi @SoqedHozi,

Are you trying to remove the characters /?%*:".<>. from message ID?

Regards,
Nimin

Correct, along with \ (which isn’t showing in the string when I paste it on here but is present in the assign activity)

I don’t actually know is / and \ appear in message IDs ever to be fair (maybe someone can tell me?), so might not need to replace them at all. However, I get the same error with 1 character in the replacement string.

Hi @SoqedHozi,

Please try this.

System.Text.RegularExpressions.Regex.Replace(mail.Headers("Message-ID"),"[<>\/\?%\\\*:\\.""]","")

Warm regards,
Nimin

1 Like

Well, the error has disappeared!

I understand the /\ and \, but I don’t understand the \.“” can you break this down for me?

Thanks

1 Like

Hi @SoqedHozi,

In order to avoid "." acting as a quantifier in a pattern, "\" is an escape character used to consider "." as a literal. Inside [ ] , '.' normally acts as a literal. but i used "\" just force it to consider as literal .

Inside the activity, "" acts as an escape character to consider " as a literal.

I hope you got it. :slightly_smiling_face:

Warm regards,
Nimin

1 Like

Running my program, this assign failed because value cannot be null. It says “parameter name: input”.

Adding the .tostring back in causes a different error on execution - assign: object reference not set to an instance of an object."

Hi @SoqedHozi,

mail.Headers(“Message-ID”) is already in string format.

Please make sure you are getting value for “Message-ID”. You can use a writeline activitymail.Headers(“Message-ID”) before doing the regex operation to verify it.

Regards,
Nimin

1 Like