First, let’s summarize the usages of the three of them:
・ replace(CharSequence target, CharSequence replacement) , replace all targets with replacement, both parameters are strings.
・ replaceAll(String regex, String replacement) , replace all regex matches with replacement. regex is obviously a regular expression, replacement is a string.
・ replaceFirst(String regex, String replacement) , which is basically the same as replaceAll, the difference is that only the first match is replaced.
Next, there is a simple requirement, which is to replace a in the source string with /a, the code is as follows:
System.out.println("abac".replace("a", "//a")); ///ab/acSystem.out.println("abac".replaceAll("a", "//a")); //abacSystem.out.println("abac".replaceFirst("a", "//a")); //abacSystem.out.println("abac".replaceFirst("a", "//a")); //abacThe result was a big surprise. After so many years of replacement, I was a little confused.
The source string is "abac", and then we find "a", replace it with /a. Since / is a java escape character, if you want to express /a, you must write "//a", the first backslash escapes the second backslash into a normal string.
With three replacement expressions, only the result of the first replace function is correct. What is the problem?
replaceAll and replaceFirst require that the first parameter is a regular expression. "a" can be understood as both the string a and the regular expression a, so the first parameter is fine.
The problem lies in the second parameter. If readers carefully read the comments of the replaceAll function, they will find the following explanation:
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 make some small tricks in the second parameter, such as a requirement: replace a in the source string with the character immediately following a, the code is as follows:
System.out.println("abac".replaceAll("a(//w)", "$1$1")); //bbccSystem.out.println("abac".replaceFirst("a(//w)", "$1$1")); //bbacAssume that the meaning of the regularity can be understood by the reader, it can be seen that in the second parameter, the $ symbol can be used to obtain the content of the group. In this example, $1 is used to obtain the content of the first group, that is, the character immediately following a.
Therefore, the $ symbol has a special meaning in the second parameter, and writing it randomly will cause an error:
System.out.println("abac".replaceAll("a(//w)", "$")); //Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1What if I just want to replace it with $? This requires escaping characters:
System.out.println("abac".replaceAll("a", "//$")); //$b$cAt this point, readers may suddenly realize that the backslash also has a special meaning (escaped) in the second parameter, so if we want to express the backslash, we must escape it again:
System.out.println("abac".replaceAll("a", "////a")); ///ab/acSystem.out.println("abac".replaceFirst("a", "/////a")); ///abacTo understand briefly, the front backslash in "////a" escapes the backslash afterwards, so that the backslash afterwards is a normal string. In this way, the string seen in java memory is "//a". When processing, the replaceAll function uses the front backslash to escape the backslash afterwards to express that the backslash afterwards is an ordinary string, not used to escape $, and the final string in memory is "/a", so that a can be successfully replaced with /a.
Summarize
The issue of escape is indeed entangled. Through this article, the author hopes that readers will be able to remain awake when using these functions in the future, be aware of the special characters in the parameters, and avoid writing time bombs. The above is the entire content of this article. I hope it will be helpful to everyone's study and work. If you have any questions, you can leave a message to communicate.