java 正则

[code="java"]
string.replaceFirst(re, "")

Pattern pattern =Pattern.compile(re);
Matcher matcher=pattern.matcher(string);
匹配的好像不太一样啊(不是指数量。。)
[/code]

String str="{ba:'bababa',a:'we',b:'w',aa:'haha'}";
String re="(?<=(\{|,)"+key+":').*?(?=')";
System.out.println(json.replaceFirst(re, ""));

和pattern.matcher(str);
结果不一样,后面的方式根本没有匹配到

如果我采用第一种方式,我怎么能返回正则所匹配的内容

java的正则表达式没办法返回一个结果数组

需要这样使用:
[code="java"]
Pattern p=Pattern.compile("a");
Matcher m=p.matcher("abcabf");
//find()会查找下一个匹配的序列
while(m.find()){
System.out.println(m.group());
}
[/code]

没办法从直接最后开始查找,只能你自已判断一下是否是最后一个匹配

[code="java"]
Pattern p=Pattern.compile("a");
Matcher m=p.matcher("abcabf");
String last=null;
while(m.find()){
last=m.group();
}
System.out.println(last);
[/code]

麻烦你把问题描述清楚好吗?看不懂你想问什么,给贴的代码甚至还有一个未知变量key

问题不清楚。