Regular Expression, about string. replaceall (..) Regular Expression, about string. replaceall (..) 2005-3-25 15:03:55 RegEx: Class T1 { Public static void main (string a []) { String S = 'e: // JBX // x9 // io9 '; String SS; // Replace '/' In Path s with '//'. Why do we need eight '/'? I thought four 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 () uses regular expression as the parameter. However, Java strings have similar processing for escape characters. First, Java will interpret "//" as a string (including two char) -- "//", which is what you see in the JDK document. Next, because replaceall uses a regular expression as the parameter, "//" is interpreted as a RegEx. For a RegEx, this represents a character, that is, "/". For the next eight/s, it will be interpreted as "//". In other words, assume that string. replaceall () is a common string instead of a RegEx parameter, so write the code as follows: String target = source. replaceall. ==================================== In the replaceall (string, string) method, brackets in the English State need to be specially processed. I found them online, you can use the following method to replace the English brackets with other characters (such as Chinese full angle brackets): str1.replaceall ("//(","("); ====================================== Java string replaceall and RegEx remove the $ symbol from a string in Java. I wrote it like this: String TMP = "-$125402.00 "; TMP. replaceall ("$ ",""); However, the execution result does not remove $. Later, I found out that I needed to write it like this. TMP. replaceall ("// $. The two parameters in string replaceall (string RegEx, string replacement) are both RegEx. Especially when the second replacement parameter is a user-input or specified string, if it contains RegEx special characters (mainly // and $) without attention, this can easily cause exceptions. In this case, replaceall is not recommended if the RegEx engine is not required to replace strings. In jdk1.5, string Replace (charsequence target, charsequence replacement) is added, which can be used. Jdk1.4 or below, do it yourself, such as oldreplace: http://www.javapractices.com/Topic80.cjpWhen I write a program, I need to write the "/" character into a text file. I thought it was not a problem. Just convert it. The Code is as follows: Targetpath = targetpath. replaceall ("//","////"); Fileoperate. createfile (filename, targetpath ); I thought there was no problem. In this way, you can first convert "/" into "//" in the read string, and then write it as "/" when writing a text file, the following error is reported: Java. util. RegEx. patternsyntaxexception: Unexpected internal error near Index 1 / After checking the information, I found that the replaceall function in Java uses a regular expression as the basis for conversion, and "/" is a special character in a regular expression. Finally, write the following code to achieve the purpose I expected: Targetpath = targetpath. replaceall ("////","////////"); Fileoperate. createfile (filename, targetpath ); |