Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing the same string gives two different values

Tags:

string

c#

regex

I am trying to parse a string and remove the 'emojis' off it and keep the new lines.

So, I have this piece of code:

string text = "S H A V A . Est 2015\nBandung\nLine: @ubm5921j\nBbm: 7D2E6310\nFAST ORDER\ud83d\udc47\ud83c\udffe\ud83d\udc47\ud83c\udffe";
MessageBox.Show(text);
string result = Regex.Replace(text, @"\p{Cs}", "");

The output of 'text' here is the following:

enter image description here

So, as you can see the new lines work fine and the end of it has 'emojis' and the next line it removes them PERFECTLY. So the result string will contain the same string with new lines and no emojis.

On another part of the program I have this code.

//uu.description is the same string as above 'text', 
//this is where I scrape directly from html
string text2 = uu.description; 
MessageBox.Show(text2);
string result2 = Regex.Replace(text2, @"\p{Cs}", "");

enter image description here

As you can see in this case, my text2 outputs the string in the format as it is, and the regex does absolutely nothing. The new lines don't work and the emojis are not removed.

I am very confused why it does work in my first case and not in the second case. I've been on this for hours and can't figure it out.

like image 755
user5204184 Avatar asked Dec 01 '25 18:12

user5204184


1 Answers

I have fixed it. My fixed code looks like this:

string text2 = uu.description;
string result2 = Regex.Replace(Regex.Unescape(text2), @"\p{Cs}", "");

For some reason, the parsed string was with an additional \, looking like this \\n. I would like to thank @stribizhev for his idea! Thank you.

like image 138
user5204184 Avatar answered Dec 04 '25 07:12

user5204184



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!