\ (backslash) and regular expressions in Java

Source: Internet
Author: User
Tags alphabetic character

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.