Trying to understand this Java string as C # code

See this java code: -

s = s.replaceAll( "\\\\", "\\\\\\\\" ).replaceAll( "\\$", "\\\\\\$" );

I donโ€™t seem to understand this. This regular expression replaces everything.

I 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'm not sure I understand why my C # code puts more \ in, mainly regarding the representation of these control characters .. ??


Update:

So, when I use the idea of โ€‹โ€‹XXX only using text.Replace(..) , this works. eg.

 text = text.Replace("\\\\", "\\\\\\\\"); text = text.Replace("\\$", "\\\\\\$"); 

But I was hoping to stick with RegEx ... to try to keep it as close to the java code as possible.

Extension Method Used ...

 public static string RegexReplace(this string input, string pattern, string replacement) { return Regex.Replace(input, pattern, replacement); } 

hmm ...

+4
source share
4 answers

Java needs all the $ characters hidden in its replacement string - "\\\\\\$" means \\ and \$ . Without it, it gives an error message: http://www.regular-expressions.info/refreplace.html (look for "$ (unescaped dollar as literal text)").

Remember $1 , $0 , etc., replace the text with the captured groups, so the second argument has part of the replaceAll syntax. C# has a slightly different syntax and does not require the extra slash, which is required literally.

You can write:

 text = text.RegexReplace(@"\\", @"\\"); text = text.RegexReplace(@"\$", @"\$"); 

Or

 text = text.RegexReplace(@"[$\\]", @"\$&"); 
+3
source

I think this is the equivalent of this C # code:

 text = text.Replace(@"\", @"\\"); text = text.Replace("$", @"\$"); 

@ indicates a string string in C #, which means that backslashes in strings should not be escaped with a lot of backslashes. In other words, the code replaces one backslash with a double backslash, and then replaces the dollar sign with a backslash followed by a dollar sign.

If you want to use the regex function, it would be something like this:

 text = text.RegexReplace(@"\\", @"\\"); text = text.RegexReplace(@"\$", @"\$$"); 

Please note that in the regex pattern (first parameter) backslashes are special, and when replacing (second parameter) these are special characters.

+1
source

The code quotes a backslash and the $ characters in the source string.

-1
source

Java Regular Expression Analysis: 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 in Java you need to escape the \ character with \, but in C # you do not. Try extracting half \ into your C # version.

-1
source

All Articles