Regular expressions, about String.replaceall (..) Java code class T1 {public static void main (string a[]) {string s = ' e:\\jbx\\x9\\io9 '; String SS; To change the ' \ ' to ' \ \ ' In the path s, why the whole 8 ' \ '. I thought 4 would do. SS = S.replaceall (' \\\\ ', ' \\\\\\\\ ') System.out.println (' s= ' + s); System.out.println (' ss= ' + ss); Output://s= E:\jbx\x9\io9//ss=e:\\jbx\\x9\\io9}}
The key here is that String.replaceall () is used as a parameter by regular expression. But the string of Java itself has similar processing for escape characters. First, Java interprets "\\\\" as a string (containing two char)--"\ \" which is what you see in the JDK's documentation.
Next, because ReplaceAll is a regular expression as an argument, "\ \" is interpreted as a regex. For a regex this represents a character, that is, "\". For the next 8, it will eventually be interpreted as "\ n".
In other words, suppose String.replaceall () is a normal string rather than a regex, so write code: String target = Source.replaceall (' \ n ', ' \\\\ '); It's OK.
===============================
In the ReplaceAll (string,string) method requires special handling of the English state of parentheses, found on the Internet, you can use the following wording to replace the English brackets with other characters (such as Chinese full-width brackets): Str1.replaceall ("" \ "", "(");
Java String ReplaceAll and Regex
In Java, you remove the $ symbol for a string. That's what I wrote:
String tmp = "-$125402.00";
Tmp.replaceall ("$", "");
But the implementation of the results did not remove the $ $. Then I found the information to write it.
Tmp.replaceall ("\\$", "") is only possible.
The two arguments in string ReplaceAll (string regex, string replacement) are regex. Especially if the second parameter replacement is a user input or a specified string, if it contains regex special characters (mainly \ and $) without attention, it is easy to cause an exception to be thrown by the problem. In this case, if it's just a simple string replacement without the Regex engine involved, don't use ReplaceAll. A string replace (charsequence target, charsequence replacement) is included in the JDK1.5 and can be used. JDK1.4 or below, make yourself a good one, like OLDREPLACE:HTTP://WWW.JAVAPRACTICES.COM/TOPIC80.CJP
When I write a program, I need to write "\" characters to a text file, start thinking that this is not a problem, the conversion is good, the code is as follows:
Thought there was no problem, so you can first read in the string of "\" into "\", and then write the text file will be written "\", did not think that the following error:
java.util.regex.PatternSyntaxException:Unexpected Internal Error near index 1
\
Back to the data found that the Java ReplaceAll function, the use of regular expressions as the basis for conversion, and "\" in the regular expression is also a special character, and finally, written in the following code to achieve my desired purpose:
The difference between the Replace and ReplaceAll methods:
The main thing is that the latter is based on a lot of substitution of rules, whereas the former is a general substitution based on character or character sequence itself.
For example, you can use ReplaceAll ("\\d", "*") to replace all the numeric characters of a string with an asterisk, and replace does not, because it is not based on rules to express many substitutions.
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.