JenJen
(Jennifer)
March 20, 2023, 6:21am
1
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?
JenJen
(Jennifer)
March 20, 2023, 6:25am
3
Basically I use the SystemException.Message in REFramework. There I have bullet points (\u2022) and new lines at the moment.
Anil_G
(Anil Gorthi)
March 20, 2023, 6:25am
4
@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
JenJen
(Jennifer)
March 20, 2023, 6:27am
5
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
Anil_G
(Anil Gorthi)
March 20, 2023, 6:29am
6
@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
JenJen
(Jennifer)
March 20, 2023, 6:32am
7
I think these are the special characters:
\u2022
\u2023
\u25E6
\u2043
\u2219
@~$^&% and new lines
Anil_G
(Anil Gorthi)
March 20, 2023, 6:35am
8
@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
JenJen
(Jennifer)
March 20, 2023, 6:38am
9
I tried regex in studio but it doesn’t recognize the characters.
Anil_G
(Anil Gorthi)
March 20, 2023, 6:38am
10
@JenJen
Can you show what you tried
Cheers
Hi @JenJen
Try this
System.Text.RegularExpressions.Regex.Replace(data, “[^a-z A-Z 0-9]”, “”)
Hope this might help you
4 Likes
Anil_G
(Anil Gorthi)
March 20, 2023, 6:46am
13
@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
JenJen
(Jennifer)
March 20, 2023, 6:47am
14
Seems like this could solve my problem, but I still need the new lines to be replaced How do I add it?
Hi @JenJen
If a string includes multiple lines, it will replace them all.
Hope this might help you
6 Likes
Yoichi
(Yoichi)
March 20, 2023, 6:52am
17
Hi,
Can you try the following expression?
yourString = System.Text.RegularExpressions.Regex.Replace(yourString,"[\u2022\u2023\u25E6\u2043\u2219@~$^&%\r\n]","")
Regards,
JenJen
(Jennifer)
March 20, 2023, 6:58am
18
Fits for the special characters but doesn’t replace/recognize new lines…
Yoichi
(Yoichi)
March 20, 2023, 7:03am
19
Hi,
In my environment, it replaces newline with blank as the following. How did you check it?
Regards,
JenJen
(Jennifer)
March 20, 2023, 7:09am
20
I restarted Studio… Seems it works now