regexp for c#, matching teams inside backspace - c#

I'm having trouble with my c# code.
I have stored a string object when I print it out it prints out "username" and when I view it in the debugger it is shown as "\"username\"". How can I replace "\ with whitespace in the variable? It is stopping me from making comparison operations.
I tried with
memberNameStripped = teamMemberName.Replace(#"\", "");
But it does not replace the "\ so how can I do it?
Thanks in advance.

Why regex? Use String.Trim to remove leading and trailing quotes("):
memberNameStripped = memberNameStripped.Trim('"');
It's efficient and clear.

The \ is an escape character, what you probably want to replace is the double quote "
So try:
memberNameStripped = teamMemberName.Replace("\"", "");

In debugger it is shown as "\"username\"" because it is a quoted string. This is why it prints out "username". You could get rid of quotes using Replace("\"", "")

Related

C#, escape double quote not working as expected

I'm trying to escape quotes in an xpath string like so:
var mktCapNode = htmlDoc.DocumentNode.SelectSingleNode("//*[#id=""yfs_j10_a""]");
The actual string I want passed is:
//*[#id="yfs_j10_a"]
This gives a compiler errors: ) expected and ; expected
I'm sure it's simple but I'm stumped. Any ideas?
You need to make this a verbatim string to use the "" as an escape
#"//*[#id=""yfs_j10_a""]"
For a normal string literal you need to use backslashes to escape the double quotes
"//*[#id=\"yfs_j10_a\"]"
Or use the escape char '\':
"//*[#id=\"yfs_j10_a\"]"
In C# the \ character is used to escape (see documentation).
This is different from VB where there are no escape characters except "" which escapes to ".
This means in C# you do not need vbCrLf to start a new line or vbTab to add a tab character to a string. Instead use "\r\n" and "\t".
You can also make the string a literal using the # character, but I do not think this works with the quotation mark.
Add the # prefix to your string.
#"//*[#id=""yfs_j10_a""]"
or escape the quotes with a \
"//*[#id=\"yfs_j10_a\"]"

\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' ";

C# Using Regular Expressions

The following works in vb.net, and basically only allows characters on a standard US Keyboard. Any other character pasted gets deleted. I use the following regular expression code:
"[^A-Za-z0-9\[\{\}\]`~!##$%\^&*\(\)_\-+=\\/:;'""<>,\.|? ]", "")
However when I try to use it in C# it won't work, I used '\' as a escape sequence. C# seems a bit different when it comes to escape sequences? Any help would be appreciated.
Prefix the string with #. That's it. From there you can use the regex string from VB as is (including doubling up on the " character).
// Note: exact same string you're using, only with a # verbatim prefix.
string regex = #"[^A-Za-z0-9\[\{\}\]`~!##$%\^&*\(\)_\-+=\\/:;'""<>,\.|? ]";
string crazy = "hĀečlĤlŁoźtƢhǣeǮrȡe";
Console.WriteLine(Regex.Replace(crazy, regex, ""));
Output:
hellothere
Prefix your string with "#" and prefix quotes within the string with "\".
I.e. this string
abc\def"hij
in C# would be encoded as
#"abc\def\"hij"
You need to escape your " character. Do this by putting a \ before your " character.
"[^A-Za-z0-9[{}]`~!##$%\^&*()_-+=\/:;'""<>,.|? ]"
should become
"[^A-Za-z0-9[{}]`~!##$%\^&*()_-+=\/:;'\"\"<>,.|? ]"
If you use the #prefix before this, it will treat the backslash literally instead of an escape character and you wont get the desired result.
Escape your characters:
"[^A-Za-z0-9[{}]`~!##$%\^&*()_-+=\\/:;'\"<>,.|? ]"
A good tool for regular expression design and testing (free) is:
http://www.radsoftware.com.au/regexdesigner/
You need to escape you regex for use in C#
[^A-Za-z0-9\[\{\}\]`~!##$%\^&*\(\)_\-+=\\/:;'\"<>,\.|? ]
Try this one!

regex can't recognize "\n"?

so at the end the end(after few days of debuging) i found a problem. It isnt in regex at all :/ . It seams that i was trimming ekstra white spaces with
intput= Regex.Replace(input, "\\s+", " ");
so all new lines are replaced with " ". Stupid! Moderator, please remove this if unnecesary!
I have regexp for tokenizing some text and it looks like this :
"(?<html>Ç)|
(?<number>\\d+(?:[.]\\d+)?(?=[][ \f\n\r\t\v!?.,():;\"'„Ç]|$))|
(?<other>(?:[^][Ç \f\n\r\t\v!?.,():;\"'„A-Za-zčćšđžČĆŠĐŽäöÖü][^ Ç\f\n\r\t\vA-Za-zčćšđžČĆŠĐŽäöÖü]*)?[^][ Ç\f\n\r\t\v!?.,():;\"'„A-Za-zčćšđžČĆŠĐŽäöÖü](?=[][!?.,():;\"'„]*(?:$|[ Ç\f\n\r\t\v])))|
(?<word>(?:[^][ Ç\f\n\r\t\v!?.,():;\"'„][^ Ç\f\n\r\t\v]*)?[^][ Ç\f\n\r\t\v!?.,():;\"'„])|
(?<punctuation>[][ \f\n\r\t\v!?.,():;\"'„])"
Problem is in this part: (?<punctuation>[][ \f\n\r\t\v!?.,():;\"'„]). So when im prsing text with input "\n\n" it is grouping in punctuation matches: " "," " - in other words, space and space... and I don't know why?
I could be wrong, but you need to hand the String as String to the RegEx...means you need to escape the backslashes.
... (?=[][ \\f\\n\\r\\t\\v!?.,():;\\" ...
Or otherwise C# will replace \n with a linebreak within the RegEx-Statement.
Edit: It's also possible to use literal strings, but the need to be marked with beginning # (see Martin's answer).
If you put an # in front of string you can use single backslashes and line-breaks will be recognized.
#"(?<html>Ç)|
Set RegexOptions.IgnorePatternWhiteSpace
Update:
Are you sure [^] is correct? Unless it's somekind of character group (that I have never used), that will be the same as . . Same goes for []. Perhaps I just have not used all of RE before :p

Replace Double Quote with Empty String C#

I have a string that look like codes=”A,B,C”, which I am getting after parsing from a word document.
At the run time I just want to replace these double quotes with empty string.
I tried doing something like str.Replace("\"", "").
But above double quotes are not getting replaced :(
As codes=”A,B,C” seems to have some different looking double quotes, not sure if that's causing the problem.
Please guide how can I replace these double quotes from above string.
Thank you!
The “ and ” characters aren't the same as the " character. How about this instead?
string clean = dirty.Replace("“", "").Replace("”", "");
The double quote character you have tried to replace is ASCII code 34. Word has used "smart quotes", which you should be able to insert in your code by holding down Alt and typing 0147 on the number keypad (the number key row won't work) and 0148.
These are the codes for those characters in the default ANSI code page for Windows I believe.
” look to me like one half of the smart quotes stuff a different thing to standard quotes. You will need to include them in your replacement code. Looking a character map they are U+201C and U+201D. Assuming the source is Unicode.
str.Replace("\"", string.Empty);

Categories