String. replaceall usage.

Source: Internet
Author: User
Document directory
  • Regular Expression, about string. replaceall (..)

Public class trydotregex {
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
String STR = "111.3.22.11 ";
STR = Str. replaceall ("(^ | //.) (// D) (//. | $)", "$100 $2 $3 ");
STR = Str. replaceall ("(^ | //.) (// d {2 })(//. | $) "," $10 $2 $3 ");
STR = Str. replaceall ("(^ | //.) (// d {2 })(//. | $) "," $10 $2 $3 ");
STR = Str. replaceall ("(^ | //.) (// d {1 })(//. | $) "," $100 $2 $3 ");

System. Out. println (STR );
}

}

Printed result: 111,003,022,011;

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.cjp

When 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 );

 

============================================

The difference between the replace and replaceall methods:

The latter is a replacement based on Rule expressions, while the former is a replacement based on characters or character sequences.

For example, replaceall ("// D", "*") can be used to replace all numeric characters in a string with asterisks, because it is not a replacement based on Rule expressions.

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.