Understanding regex in Java: split(“\t”) vs split(“\\t”) - when do they both work, and when should they be used

后端 未结 2 481
一生所求
一生所求 2020-11-29 09:10

I have recently figured out that I haven\'t been using regex properly in my code. Given the example of a tab delimited string str, I have been using str.s

2条回答
  •  -上瘾入骨i
    2020-11-29 09:49

    When using "\t", the escape sequence \t is replaced by Java with the character U+0009. When using "\\t", the escape sequence \\ in \\t is replaced by Java with \, resulting in \t that is then interpreted by the regular expression parser as the character U+0009.

    So both notations will be interpreted correctly. It’s just the question when it is replaced with the corresponding character.

提交回复
热议问题