Today, I saw an interesting question in the question and answer area and moved directly to the following: (Http://www.oschina.net/question/2268249_218189#tags_nav)
The regular expressions that appear to be explicitly entered are different, but the output is the same (tabs are replaced). Then went to check the pattern of the doc, found the following passage:
It is an error to use a backslash prior to any alphabetic character this does not denote an escaped construct; These is reserved for future extensions to the Regular-expression language. A backslash may used prior to a non-alphabetic character regardless of whether that character was part of a unescaped C Onstruct.
The main idea is that a backslash can be placed before any of the non-alphabetic characters, regardless of whether the character is a character that can be escaped. (The test found that if the backslash followed by a non-alphabetic character is not any effect, with no backslash effect)
Then look back to this problem, because \ in Java is escape character, so there is a \ where we do a conversion as follows, the left is the original input, the right is escaped after the string array (the form of the array more convenient to distinguish):
- \ t, {\ t}
- \ t-and {\,t}
- \ \ t, {\, \ t}
- \ \ \ t, {\, \, T}
The first is a tab, so it is understandable to be replaced.
The second escape, though not a tab, is literally a tab, so the regular replacement is also replaced.
The third escape is after a \ and a tab, according to what Javadoc mentioned earlier. A tab is a non-alphabetic character, so this usage actually represents a tab (you can also change \ t to \ You see the output).
The fourth escape is \,\ and T three characters, so the replacement condition is obviously not satisfied.
\ (backslash) and regular expressions in Java