【发布时间】:2009-12-15 20:44:40
【问题描述】:
考虑这段代码:
import java.util.regex.*;
public class Pattern3 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Pattern p = Pattern.compile("Our"); //line 1
Matcher m = p.matcher("Our mom and Our dad"); //line 2
//p.compile(mom); commented line
StringBuffer s = new StringBuffer();
boolean found = m.find();
while (found){
m.appendReplacement(s, "My"); //line 3
found=m.find();
}
m.appendTail(s); //line 4
System.out.println(s);
}
}
a) 为什么我需要在第 4 行调用 m.appendTrail(s) 来获取未截断的字符串?
b) 当我取消注释并将"mom" 放置为新的正则表达式时,为什么输出没有变化?
【问题讨论】: