Im getting funny % signs back how do I get rid of them? - c#

Im trying to parse a response from an authentication server that is url encoded. However when I do it I am getting \r\n characters. And I need to not have these in my text as I need to be able to run a regular expression that looks for white space but doesnt work with these escape characters.
So basically the string returned is:
ClientIP=192.168.20.31%0d%0aUrl%3d%2fflash%2f56553550_hi%3funiqueReference%3d27809666.mp4
After url decoding it it is:
192.168.20.31\r\nUrl=/flash/56553550_hi?uniqueReference=27809666.mp4
So you see I dont want it to have \r \n etc I want to have:
"192.168.20.31 Url=/flash/56553550_hi?uniqueReference=27809666.mp4"
As a verbatim string in c#.
Is this possible? Or do I have to do a string.replace on \r and replace with " "?

Either replace %0d%0a with %20 before URL decoding the string, or the \r\n with after.
Since the data exists in the original string, you can't just make it go away without replacing it.

Related

C# regex.escape unexpected behavior when processing "."

Hey I have an issue with Regex.Escape I'm trying to feed it an Email from TextBox Controll. The function recieves "test#test.test". What I expect to get is this "test#test\.test" Regex.Escape escapes the dot character. Hovever what I get instead is "test#test\\.test" which is very confusing. I plan on handing that string down to an SQL query and I'm worried abut users misbehaving.
holder.address = Regex.Escape(EmailAddressInput.Text);
This is how I assign resulting string to field in holder class.
I have been researching this problem on my own but most sources (including MSDN) suggest to prefix the dot ("the special character") with one backslash.
As it is right now backslash escapes backslash and result is a badly formatted email address.
var s = "test#test\\.test"; means the s holds the test#test\.test string. Your issue does not exist. There is a single backslash. Click the magnifier button on the right - you will see that in the Text Visualizer.
Regex has to have \\ because its escaping the \
the string itself actually only has one \ in it.

deserialize xml attribute and handle newline and other special characters

I've tried finding the answer to this for the last 2 days and I just can't find anything that will work with our code.
We have an incoming xml response formatted as below and need to be able to handle newline and other special characters inside of attributes.
The one we're having issues with is "agent-notes" we can not seem to be able to find an XPath function to convert the special characters into \r \n etc.
"anything
everything
something" should be "anything \r \n everything \r \n something"
Unfortunetly, you can't. The agent property value is valid and cannot be assumed to be converted for you in XPath search. You will have to convert you search path by replacing all \n\r to "
". If its the value that you are expecting to be converted then you can use "HttpUtility.HtmlDecode Method".
I've had this problem before and suffered the same fustration as. Coding is not always a perfect science, as much as you would like it to be.

Escape string from file

I have to parse some files that contain some string that has characters in them that I need to escape. To make a short example you can imagine something like this:
var stringFromFile = "This is \\n a test \\u0085";
Console.WriteLine(stringFromFile);
The above results in the output:
This is \n a test \u0085
, but I want the text escaped. How do I do this in C#? The text contains unicode characters too.
To make clear; The above code is just an example. The text contains the \n and unicode \u00xx characters from the file.
Example of the file contents:
Fisika (vanaf Grieks, \u03C6\u03C5\u03C3\u03B9\u03BA\u03CC\u03C2,
\"Natuurlik\", en \u03C6\u03CD\u03C3\u03B9\u03C2, \"Natuur\") is die
wetenskap van die Natuur
Try it using: Regex.Unescape(string)
Should be the right way.
Att.
Don't use the # symbol -- this interprets the string as 100% literal. Just take it off and all shall be well.
EDIT
I may have been a bit hasty with my reply. I think what you're asking is: how can I have C# turn the literal string '\n' into a newline, when read from a file (similar question for other escaped literals).
The answer is: you write it yourself. You need to search for "\\n" and convert it to "\n". Keep in mind that in C#, it's the compiler not the language that changes your strings into actual literals, so there's not some library call to do this (actually there could be -- someone look this up, quick).
EDIT
Aha! Eureka! Behold:
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.unescape.aspx
Since you are reading the string from a file, \n is not read as a unicode character but rather as two characters \ and n.
I would say you probably need a search an replace function to convert string "\n" to its unicode character '\n' and so on.
I don't think there's any easy way to do this. Because it's the job of lexical analyzer to parse literals.
I would try generating and compiling a class via CodeDOM with the string inserted there as constant. It's not very fast but it will do all escaping.

Removing String Escape Codes

My program outputs strings like "Wzyryrff}av{v5~fvzu: Bb``igbuz~+\177Ql\027}C5]{H5LqL{" and the problem is the escape codes (\\\ instead of \, \177 instead of the character, etc.)
I need a way to unescape the string of all escape codes (mainly just the \\\ and octal \027 types). Is there something that already does this?
Thanks
Reference: http://www.tailrecursive.org/postscript/escapes.html
The strings are an encrypted value and I need to decrypt them, but I'm getting the wrong values since the strings are escaped
It sounds more like it's encoded rather than simply escaped (if \177 is really a character). So, try decoding it.
There is nothing built in to do exactly this kind of escaping.
You will need to parse and replace these sequences yourself.
The \xxx octal escapes can be found with a RegEx (\\\d{3}), iterating over the matches will allow you to parse out the octal part and get the replacement character for it (then a simple replace will do).
The others appear to be simple to replace with string.Replace.
If the string is encrypted then you probably need to treat it as binary and not text. You need to know how it is encoded and decode it accordingly. The fact that you can view it as text is incidental.
If you want to replace specific contents you can just use the .Replace() method.
i.e. myInput.Replace("\\", #"\")
I am not sure why the "\" is a problem for you. If it its actually an escape code then it just should be fine since the \ represents the \ in a string.
What is the reason you need to "remove" the escape codes?

Why do I get literal \r and \n when getting text from a database using C#?

In a database field, I'm storing and returning the "body" of my email (in case it changes). In this I have \n\r characters to get new lines. But, it seems not to be working. Any thoughts:
So data in field:
'TBD.\r\n\nExpect'
And my output looks like (literal \r and \n):
TBD.\r\n\nExpect
Thoughts?
Escape sequences have no meaning within actual string objects - only when the C# parser/compiler interprets them. You want to store an actual new line in your database field rather than the 4 characters "\r\n".
It is likely that the \r\n is escaped, so the string actually returned would be equivalent to a string
myString = "\\r\\n";
So you would need to remove these extra slashes either when adding or removing from the database.
Though likely unrelated to your problem, the extra \n you have may cause viewing problems depending on the system, editor, etc.
You could replace all occurrences of \\n\\r, etc. using:
replacedString = myString.Replace("\\r\\n", "\r\n");
This should work to fix your problem.
Because \r, \n, etc. works only within a string in your C# code. If you read a string from a file, a database, or other things, they just get the verbatim values...
Replace your \r\n with System.Environment.NewLine as the below may do work for you:
text.Replace("\r\n", System.Environment.NewLine);

Categories