Escape character in c# - c#

I stuck in escape sequence in program.
I am getting input from xml file in single quotes.
So in string it may contain any escape sequence.
So what I want to do is consider next character after escape character as normal character.
Example: '\abc\\t'
Output: 'abc\t'
So for that I created regular expression which is:
Regex.Replace(SearchString, #"\\?(?<Character>)", "${Character}");
But it replaces all escape character and gives output :
abct
Please help me how can I do it?

The named group in your regular expression matches nothing, so it does the same as Replace(#"\", "").
Match one character in the group:
SearchString = Regex.Replace(SearchString, #"\\(.)", "$1");

Basically, you need parse your string character by character. Pseudo-code:
Loop through all characters c:
If c is escape character:
Read next character and do something special (\t is tab, \\ is backslash, ...)
Else:
Copy character to output

Try this:
Regex.Replace(SearchString, #"\\(?<Character>[^\\])", "${Character}");

What if you do like:
SearchString = SearchString.Replace(#"\\", #"\");
It would give:
\abc\t
Here escaped \t is now treated as a "tab character".
It is what you are looking for ?

Related

How to remove slashes at beginning of string but not any in the middle

I'm trying to figure out how I can remove only the slashes (forward and backward) that occur at the beginning of the string below, up until the first letter or number. I don't want to remove any slashes in the middle of the string.
string: "\//hello\how\are/you"
looking for result like this: "hello\how\are/you"
Thanks!
Jason
You can use this pattern:
#"^[/\\]+"
It is a very basic pattern:
^ means start of the string
[/\\] is a character class that contains / and \ (note that you must escape the backslash to not escape the closing square bracket)
string = string.TrimStart('\\', '/');
As an added bonus, you need not use a regex for this purpose.

\tssr>"&\8=f23' as String C#

I have a short question.
I want do put this \tssr>"&\8=f23' into a String (Language C#).
But the compiler always shows an error because for example "\8" is a command.
can someone help me?
Thank you very mutch.
string s = "\\tssr>\"&\\8=f23'";
OR
string s = #"\tssr>""&\8=f23'";
try
string s = #"\tssr>\"&\8=f23";
For double quote[EDIT]
string s = #"\tssr>""&\8=f23";
because \ is special char you need to escape it with either # for \\ as given in below answer
Just write \\8 instead of \8. Or put an # in front of the string. Other characters also need to be escaped with the \ character:
"\\tssr>\"&\\8=f23'"
or this:
#"\tssr>""&\8=f23'"
the backslash \ is used for escaping special characters, like tab or newline. Because of that, the first character also needs to be escaped, because \t is the escape code for Tab.
Try escape sequence
Check this
\\tssr>\"&\\8=f23\'
See below.
var str = "\\tssr>\"&\\8=f23'";
I add a backslash to escape the special characters
The backslash is an escape character in C#, which forms part of an escape sequence.
You have two options: either use TWO backslashes (also known as escaping the backslash) for example var foo = "hello\\world";, or embed the sequence into a string literal eg var foo = #"hello\world";.
Try this:
string s = " \\tssr>\"&\\8=f23' ";

Regex.IsMatch() not recognizing escape sequence?

I'm doing a match comparison on some escaped strings:
Regex.IsMatch("\\Application.evtx", "DebugLogs\\ConfigurationServices.log");
I don't see why I'm getting:
"parsing "DebugLogs\ConfigurationServices.log" - Unrecognized escape sequence \C."
The \C is escaped?
The edit really fooled a lot of people, including me!
'\' is a special character in regular expressions - it effectively is an escape character or denotes an escape sequence.
So the RegEx engine sees DebugLogs*\C*onfigurationServices.log which is, indeed, an unrecognized escape sequence. \A actually is an existing escape sequence.
So you need to escape the escape character. The simplest way to do this is to double the number of slashes used:
Regex.IsMatch("\\\\Application.evtx", "DebugLogs\\\\ConfigurationServices.log");
Which the RegEx engine will see as comparisons betweeen "\\Appplication.evtx" and "DebugLogs\\ConfigurationServices.log" - now the backslash has been escaped and has no special meaning.
Regex.IsMatch(#"\\Application.evtx", #"DebugLogs\\ConfigurationServices.log");
works fine too and is more readable.
The \ character is the escape character in strings. For example if you'd like to do a carriage return, you'd use \r. To get around this either use literal strings
#"\Application.evtx"
Or escape the escape character
"\\Application.evtx"
You probably want
Regex.IsMatch(#"\Application.evtx", #"DebugLogs\ConfigurationServices.log");
without the "#" C# will treat \C as an escape sequence similar to the way it would convert \n in to a newline character however \C is not a recognised/valid escape sequence.

How do you use this # in C#?

simpleSound = new SoundPlayer(#"c:\Windows\Media\chimes.wav");
Why and what is the # for?
It's a literal string. Instead of having to escape the "\" by putting two of them "\" the compiler interprets the string "as is".
Say you wanted to print out the following text to the screen: "Hello \t world".
If you were to just do Console.WriteLine("Hello \t world"), then your output would be:
Hello world
notice the tab. That's because \t is interperted as a tab. If you use the literal though, like this:
Console.WriteLine(#"Hello \t world")
then your output would be:
"Hello \t world"
The # sign before a string means to treat the backslash as a normal character rather than the start of a special character (such as newline).
It identifies a string literal. It allows you to have the special character \ in the string without escaping it.
It is a verbatim string. A verbatim string allows you to include special characters like \, " etc. without using the escape sequence.
Another usage of # is that you can put it in front of a keyword, if you want to use a keyword as a variable name (even if it's not a good idea). Example:
public int #object = 1;
Defining the # symbol prior to the assigning a string value will prevent the need for doubling the backslash (\) in C#. The at symbol (#) simply ignores escape characters.
"c:\\my\\file.txt" could be done as #"c:\my\file.txt"
This means your \n and \t or whatever it is will also not be processed.

Finding C#-style unescaped strings using regular expressions

I'm trying to write a regular expression that finds C#-style unescaped strings, such as
string x = #"hello
world";
The problem I'm having is how to write a rule that handles double quotes within the string correctly, like in this example
string x = #"before quote ""junk"" after quote";
This should be an easy one, right?
Try this one:
#".*?(""|[^"])"([^"]|$)
The first parantheses mean 'If there is an " before the finishing quote, it better be two of them', the second parantheses mean 'After the finishing quote, there sould ether be not a quote, or the end of the line'.
How 'bout the regex #\"([^\"]|\"\")*\"(?=[^\"])
Due to greedy matching, the final lookahead clause is likely not to be needed in your regex engine, although it is more specific.
If I remember correctly, you have to use \"" - the double-double quotes to hash it for C# and the backslash to hash it for regex.
Try this:
#"[^"]*?(""[^"]*?)*";
It looks for the starting characters #", for the ending characters "; (you can leave the semicolon out if you need to) and in between it can have any characters except quotes, or if there are quotes they have to be doubled.
#"(?:""|[^"])*"(?!")
is the right regex for this job. It matches the #, a quote, then either two quotes in a row or any non-quote character, repeating this up unto the next quote (that isn't doubled).
"^#(""|[^"])*$" is the regex you want, looking for first an at-sign and a double-quote, then a sequence of any characters (except double-quotes) or double double-quotes, and finally a double-quote.
As a string literal in C#, you'd have to write it string regex = "^#\"(\"\"|[^\"])*\"$"; or string regex = #"^#""(""""|[^""])*""$";. Choose your poison.

Categories