★ReplaceAll ()/appendReplacement ()/appendTail ():
The Matcher class also provides four methods to replace matched substrings with specified strings:
- Replaceall ()
- Replacefirst ()
- AppendReplacement ()
- 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 ());}} |