求简单正则表达式字符串替换~~~
例如:
myBean[3].student[1].score[5].value
替换结果
myBean[4].student[2].score[6].value
其实就是把上面的[]里的数字+1替换。
原因我在使用
http://commons.apache.org/jxpath/
但里面index是开始是1,不是0.
------解决方案--------------------
这个试试
String str = "myBean[3].student[1].score[5].value";
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());
------解决方案--------------------
Java code
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);
}