Escaping all special characters at once

Hey hey,

is there a command to escape all special characters in a string variable? My string variable is dynamic. Sometimes it contains special bullet points, sometimes new lines, etc. These have to be escaped dynamically. After that I want to use this string as part of the body in a HTTP Request.

I tried Regex already, but it didn’t work.

Hi @JenJen ,

Can you share the string containing the special characters for our reference?

Basically I use the SystemException.Message in REFramework. There I have bullet points (\u2022) and new lines at the moment.

@JenJen

How do you want to escape them?

\W can be used in regex to identify all the non word characters

You can as well use urlencode if needed to encode special characters as url or urldecode to change them to normal characters

Httputility.URLEncode(stringvar)

HttpUtility.URLDecode(stringvar)

Cheers

I don’t want to escape all non word chars (Dots, commas, etc should be excluded from that.

Especially “escaping”/replacing new lines seems to be challenging :smiley:

@JenJen

Let me know what needs to be removed from the escape characters we can add them as exceptions…\W takes care of new line characters as well…for all space characters we can use \s which includes newline character as well

Cheers

I think these are the special characters:

\u2022
\u2023
\u25E6
\u2043
\u2219
@~$^&% and new lines

@JenJen

Please check this…I added few…can add more to the escape characters inside the square brackets

"[^\w,@/\^]"

Hope this helps

Cheers

1 Like

I tried regex in studio but it doesn’t recognize the characters. :confused:

@JenJen

Can you show what you tried

Cheers

image

Hi @JenJen

Try this
System.Text.RegularExpressions.Regex.Replace(data, “[^a-z A-Z 0-9]”, “”)

Hope this might help you :slight_smile:

4 Likes

@JenJen

I see it is matching what all you neede right?

You dint want @ so it did not match…May I know what difference you are seeing?

Cheers

Seems like this could solve my problem, but I still need the new lines to be replaced :smiley: How do I add it?

Hi @JenJen

image

If a string includes multiple lines, it will replace them all.

Hope this might help you :slight_smile:

6 Likes

@JenJen

Try this

[A-Za-z0-9\s]

Cheers

1 Like

Hi,

Can you try the following expression?

yourString = System.Text.RegularExpressions.Regex.Replace(yourString,"[\u2022\u2023\u25E6\u2043\u2219@~$^&%\r\n]","")

Regards,

Fits for the special characters but doesn’t replace/recognize new lines…

Hi,

In my environment, it replaces newline with blank as the following. How did you check it?

Regards,

I restarted Studio… Seems it works now :wink: