Why String.replaceAll() in java requires 4 slashes “\\\\” in regex to actually replace “\”?

后端 未结 6 1471
攒了一身酷
攒了一身酷 2020-11-27 13:17

I recently noticed that, String.replaceAll(regex,replacement) behaves very weirdly when it comes to the escape-character \"\\\"(slash)

For example consider there is

6条回答
  •  孤独总比滥情好
    2020-11-27 14:03

    1) Let's say you want to replace a single \ using Java's replaceAll method:

       \
       ˪--- 1) the final backslash
    

    2) Java's replaceAll method takes a regex as first argument. In a regex literal, \ has a special meaning, e.g. in \d which is a shortcut for [0-9] (any digit). The way to escape a metachar in a regex literal is to precede it with a \, which leads to:

     \ \
     | ˪--- 1) the final backslash
     |
     ˪----- 2) the backslash needed to escape 1) in a regex literal
    

    3) In Java, there is no regex literal: you write a regex in a string literal (unlike JavaScript for example, where you can write /\d+/). But in a string literal, \ also has a special meaning, e.g. in \n (a new line) or \t (a tab). The way to escape a metachar in a string literal is to precede it with a \, which leads to:

    \\\\
    |||˪--- 1) the final backslash
    ||˪---- 3) the backslash needed to escape 1) in a string literal
    |˪----- 2) the backslash needed to escape 1) in a regex literal
    ˪------ 3) the backslash needed to escape 2) in a string literal
    

提交回复
热议问题