例如:
myBean[3].student[1].score[5].value替换结果
myBean[4].student[2].score[6].value其实就是把上面的[]里的数字+1替换。原因我在使用
http://commons.apache.org/jxpath/
但里面index是开始是1,不是0.
myBean[3].student[1].score[5].value替换结果
myBean[4].student[2].score[6].value其实就是把上面的[]里的数字+1替换。原因我在使用
http://commons.apache.org/jxpath/
但里面index是开始是1,不是0.
Pattern pattern = Pattern.compile("\\[(\\d+)\\]");
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while(matcher.find()){
int value = Integer.parseInt(matcher.group(1));
matcher.appendReplacement(sb, String.valueOf(value + 1));
}
matcher.appendTail(sb);
System.out.println(sb.toString());
public static void main(String[] args) {
String str = "myBean[3].student[1].score[5].value";
Pattern pattern = Pattern.compile("\\[(\\d+)\\]");
Matcher m = pattern.matcher(str);
while(m.find()){
str = str.replace(m.group(), "[" + (Integer.parseInt(m.group(1))+1) + "]");
}
System.out.println(str);
}
str = "myBean[3].student[4].score[5].value"
你看什么结果