Do you really use the Java replaceall function?

Source: Internet
Author: User

Replace, ReplaceAll, Replacefirst These three functions will be Java's classmate estimates have used, the author has been used for more than 2 years, but, we really understand them?

Summarize the usage of their three:

· Replace (charsequence target, charsequence replacement), replacing all target with replacement, two parameters are strings.

· ReplaceAll (string regex, string replacement), replacing all regex matches with replacement, the Regex is obviously a regular expression, and replacement is a string.

· Replacefirst (string regex, string replacement), basically the same as ReplaceAll, except that only the first match is replaced.

And then there's a simple need to replace a in the source string with \a, the code is as follows:

1 // \ab\ac 2 // Abac 3 // Abac

The result is a surprise, with so many years of replacement, unexpectedly a little blindfolded.

The source string is "Abac", then we find "a", replace it with \a, because \ is the Java escape character, so want to express \a must be written as "\\a", the first backslash escaped the second backslash into a normal string.

Three substitution expressions, only the result of the first replace function is correct, what is the problem?

ReplaceAll and Replacefirst require the first parameter to be a regular expression, and "a" can be understood either as a string a or as a regular expression, so the first parameter is OK.

The problem is on the second parameter, and if the reader reads the comments of the ReplaceAll function carefully, it will find the following description:

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; See Matcher.replaceall. Use Java.util.regex.Matcher.quoteReplacement to suppress the special meaning of these characters, if desired.

Since the first parameter of ReplaceAll and Replacefirst is regular, we can do a few tricks in the second argument, such as a requirement to replace a in the source string with a character immediately behind a, and the code is as follows:

1 // BBCC 2 // Bbac

The meaning of the regular assumes that the reader can read, and it can be seen that in the second argument, the contents of the grouping can be obtained with the $ symbol, in this case the first grouped content, which is the character immediately behind a.

Therefore, the $ symbol has a special meaning in the second argument, and the scribble will be error-sensitive:

1 // Exception in thread "main" java.lang.StringIndexOutOfBoundsException:String index out of range:1

What if I want to replace it with $? This requires the escape character:

1 // $b $c

Here, the reader may suddenly realize that the backslash has a special meaning (escape) in the second parameter, so if we want to express a backslash, we have to escape it again:

1 // \ab\ac 2 // \abac

Simply understand that the backslash in the front of "\\\\a" escapes the back slash, so that the trailing backslash is the normal string, so that the string seen in Java memory is "\\a", and then the ReplaceAll function is processed, and then the backslash is escaped by the front backslash, To express the trailing backslash is a normal string, not to escape $, the final memory of the string is "\a", so that you can successfully replace a to \a.

Escaping the problem really tangled, through this article, I hope that the reader later use these functions, can stay awake, be able to realize the special characters in the parameters, avoid writing time bombs.

Do you really use the Java replaceall function?

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.