How to escape $ in java?

前端 未结 4 1471
别那么骄傲
别那么骄傲 2021-01-19 09:17

I am trying below code but getting error

String x = \"aaa XXX bbb\";
    String replace = \"XXX\";
    String y = \"xy$z\";
    String z=y.replaceAll(\"$\",          


        
4条回答
  •  情深已故
    2021-01-19 09:53

    The problem id due to replaceFirst The value of String z=y.replaceAll("$", "\\$"); is xy$z$

    Replaces the first substring of this string that matches the given regular expression with the given replacement. An invocation of this method of the form str.replaceFirst(regex, repl) yields exactly the same result as the expression

    Pattern.compile(regex).matcher(str).replaceFirst(repl)
    

    Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string;

提交回复
热议问题