Why does replaceAll fail with “illegal group reference”?

前端 未结 8 1556
轻奢々
轻奢々 2020-12-01 09:56

I am in need to replace

\\\\\\s+\\\\$\\\\$ to $$

I used

String s = \"  $$\";
s = s.replaceAll(\"\\\\s+\\\\$\\\\$\",\"$$\"         


        
8条回答
  •  伪装坚强ぢ
    2020-12-01 10:44

    $ has special meaning in the replacement string as well as in the regex, so you have to escape it there, too:

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

提交回复
热议问题