Trying to understand this line of Java, as C# code
See this java code :-
s = s.replaceAll( "\\\\", "\\\\\\\\" ).replaceAll( "\\$", "\\\\\\$" );
I sorta don't understand it. It's a regex replace all.
I've tried the following C# code...
text = text.RegexReplace("\\\\", "\\\\\\\\");
text = text.RegexReplace("\\$", "\\\\\\$");
But if i have the following unit test :-
} ul[id$=foo] label:hover {
The java code returns: } ul[id\$=foo] label:hover {
My c# code returns: } ul[id\\\$=foo] label:hover {
So i'开发者_JS百科m not sure I understand why my c# code is putting more \
's in, mainly with regards to how these control characters are being represented.. ??
Update:
So, when i use XXX's idea of just using text.Replace(..)
, this works.
eg.
text = text.Replace("\\\\", "\\\\\\\\");
text = text.Replace("\\$", "\\\\\\$");
But I was hoping to stick with RegEx... to try and keep it as close to the java code as possible.
The extension method being used is...
public static string RegexReplace(this string input,
string pattern,
string replacement)
{
return Regex.Replace(input, pattern, replacement);
}
hmm...
Java needs all $
signs escaped in its replace string - "\\\\\\$"
means \\
and \$
. Without it it throws an error: http://www.regular-expressions.info/refreplace.html (look for "$ (unescaped dollar as literal text)").
Remember $1
, $0
etc are replaced the text with captured groups, so there are a part of the syntax on the second argument to replaceAll
. C#
has a slightly different syntax, and doesn't require the extra slash, which it takes literally.
You could write:
text = text.RegexReplace(@"\\", @"\\");
text = text.RegexReplace(@"\$", @"\$");
Or,
text = text.RegexReplace(@"[$\\]", @"\$&");
I think it's the equivalent of this C# code:
text = text.Replace(@"\", @"\\");
text = text.Replace("$", @"\$");
The @
indicates a verbatim string in C#, meaning that the backslashes in strings don't have to be escaped with more backslashes. In other words, the code replaces a single backslash with a double backslash and then replaces a dollarsign with a backslash followed by a dollarsign.
If you were to use the regex function, it would be something like this:
text = text.RegexReplace(@"\\", @"\\");
text = text.RegexReplace(@"\$", @"\$$");
Note that in the regex pattern (the first parameter), backslashes are special, while in the replacement (the second parameter) it is the dollarsigns that are special.
The code quotes the backslashes and '$' characters in the original string.
Java regex parsing: http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
C#: http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx
I think that in Java, you have to escape the \ character by using \, but in C#, you don't. Try taking out half of the \ in your C# version.
精彩评论