public static void main(String args[])
{
System.out.println(getRegExpValByIdx("[0-9]+","%$^%11 12113$Goodfdf678())",2));
}public static String getRegExpValByIdx(String regExp,String oriStr,int idx)
{
Pattern p=Pattern.compile(regExp);
Matcher matcher=p.matcher(oriStr);
matcher.start(idx);
String result=null;
if(matcher.find(idx))
result=matcher.group();
return result;
}这个getRegExpValByIdx是想返回第idx个匹配的字符串, 无论idx参数设为什么,都只返回第一个匹配的的字符串.
{
System.out.println(getRegExpValByIdx("[0-9]+","%$^%11 12113$Goodfdf678())",2));
}public static String getRegExpValByIdx(String regExp,String oriStr,int idx)
{
Pattern p=Pattern.compile(regExp);
Matcher matcher=p.matcher(oriStr);
matcher.start(idx);
String result=null;
if(matcher.find(idx))
result=matcher.group();
return result;
}这个getRegExpValByIdx是想返回第idx个匹配的字符串, 无论idx参数设为什么,都只返回第一个匹配的的字符串.
think in java 各章后练习答案.....
http://blog.csdn.net/heimaoxiaozi/
Pattern p = Pattern.compile(regExp);
Matcher matcher = p.matcher(oriStr);
// matcher.start(idx); 在分组的模式下,此方法是返回以前匹配的匹配操作期间,由给定组所捕获的子序列的初始索引。
String result = null;
for(int i=0;matcher.find();i++){
if(i==idx){
result = matcher.group();
}
}
return result;
}
===================================
这个名字起得好
public static void main(String args[])
{
System.out.println(getRegExpValByIdx("[0-9]+","%$^%11 12113$Goodfdf678())",3));
}public static String getRegExpValByIdx(String regExp,String oriStr,int idx)
{
Pattern p=Pattern.compile(regExp);
Matcher matcher=p.matcher(oriStr); String result=null;
int index = 1;
while(matcher.find()){
if(idx == index){
result=matcher.group();
break;
}
index++;
}
return result;
}