Eight backslashes required to replace single backslash with double backslashes?

后端 未结 6 514
借酒劲吻你
借酒劲吻你 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:14

    As a fan of not getting into super detailed explanations of regex... I figured out from the major answer post by Bart Kiers above:

    System.out.println( "line1: "+"hello\\\\world" );
    System.out.println( "line2: "+"hello\\\\world".replaceAll("\\\\\\\\", Matcher.quoteReplacement("\\") ) );
    

    prints out

    line1: hello\\world
    line2: hello\world
    

    I hope it helps...

提交回复
热议问题