Replacement of java Regular Expressions

Source: Internet
Author: User

★ReplaceAll ()/appendReplacement ()/appendTail ():
The Matcher class also provides four methods to replace matched substrings with specified strings:

  1. Replaceall ()
  2. Replacefirst ()
  3. AppendReplacement ()
  4. AppendTail ()


Public class Test {
 
/**
* @ Param args
* @ Return
* 4
* 1240
* 124067
*/
Public static void main (String [] args ){
String string = "1234567 ";
Matcher = Pattern. compile ("3 (4) 5"). matcher (string );
If (matcher. find ()){
System. out. println (matcher. group (1 ));
StringBuffer sb = new StringBuffer ();
Matcher. appendReplacement (sb, matcher. group (1) + "0"); // Replace the entire group ()

Matcher. appendReplacement (sb, "$0" + "$1" + "_ recycle /");

System. out. println (sb );
Matcher. appendtail (SB );
System. Out. println (sb. tostring ());

}
}

}

 

Replaceall () and replacefirst () are easy to use. Please refer to the explanation of the above method. We mainly focus on the appendreplacement () and appendtail () methods.

Appendreplacement (stringbuffer Sb, string replacement) replaces the current matched substring with the specified string, in addition, the replaced substring and its string segments after the matched substring are added to a stringbuffer object, while appendtail (stringbuffer SB) the method adds the remaining strings after the last matching operation to a stringbuffer object.

For example, if there is a string fatcatfatcatfat and the regular expression pattern is "cat" and appendreplacement (SB, "dog") is called after the first match, then the content of stringbuffer Sb is fatdog, that is, CAT in fatcat is replaced with dog and added to sb before matching the substring. After the second matching, appendreplacement (SB, "dog") is called "), then the content of Sb becomes fatdogfatdog. If you call appendtail (SB) Again, the final content of Sb will be
Fatdogfatdogfat.

Still a little fuzzy? Let's look at a simple program:

// Change "Kelvin" in the sentence to "Kevin" Import Java. util. regEx. *; public class matchertest {public static void main (string [] ARGs) throws exception {// generate a pattern object and compile a simple regular expression "Kelvin" pattern P = pattern. compile ("Kevin"); // use the matcher () method of the pattern class to generate a matcher object matcher M = P. matcher ("Kelvin Li and Kelvin Chan are both working in" + "Kelvin Chen's kelvinsoftshop company"); stringbuffer sb = new stringbuffer (); int I = 0; // use the find () method to find the first matched object boolean result = m. find (); // use a loop to locate and replace all Kelvin in the sentence and add the content to sb. While (result) {I ++; M. appendreplacement (SB, "Kevin"); system. out. println ("+ I +" the content of the SB after the second match is: "+ Sb); // continue to find the next matching object result = m. find ();} // finally call the appendtail () method to add the remaining string after the last match to sb; M. appendtail (SB); system. out. println ("call m. the final content of Sb after appendtail (SB) is: "+ sb. tostring ());}}

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.