Eight backslashes required to replace single backslash with double backslashes?

后端 未结 6 462
借酒劲吻你
借酒劲吻你 2021-01-18 09:37

This is a \"what the heck is going on here\" question. I don\'t actually need a solution.

I had to replace all single backslashes in a String with double backslashe

6条回答
  •  太阳男子
    2021-01-18 10:18

    The second argument isn't a regex-string, but a regex-replacement-string, in which the backslash also has a special meaning (it is used to escape the special character $ used for variable interpolation and is also used to escape itself).

    From The API:

    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; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.

    -- http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(...)

提交回复
热议问题