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

后端 未结 2 462
一生所求
一生所求 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条回答
  • 2020-11-29 09:32

    \ is consider to be escape char in java, so to get correct regex you need to escape \ with \ and t to indicate tab.

    This tutorial will help more

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题