java - replaceAll regular expression Replacing $ -


I am trying to replace all the $ characters in string expression

An example string is $ $ S with S and the other with the end and here's the end.

so that the $ letters are surrounded by spaces

I've tried string.replaceAll ("$", "$"); This is the result of an invalid argument exception.

When I try to escape $ character like this:

  string.replaceAll ("\ $", "$"); Before I even build, I get an invalid escape sequence error.   

When I try the following: prefix string.replaceAll ("\\ $", "$"); I get an invalid argument exception again

Finally when I try this:

  string.replaceAll ("\\\\ $" , "$");   

It does not have any effect on string. I know it's stupid that I am not getting anybody here anybody can help?

You will need two slashes on both sides

  string.replaceAll ("\\ $", "\\ $");   

The first one is avoided by a second slash which will be passed on to regular expressions, then the expression "$$" matches the $ sign. And you want to change it with that.

You must also avoid the second parameter because all though this is not a regular expression \ $ $ and the $ sign according to the document is a special case:

Note that The result of the backslash () and the dollar sign ($) in the replacement string can be different if it is considered to be a literal replacement string; View Matcher.replaceAll Use Matcher.quoteReplacement (java.lang.String) to suppress these special words.

Comments