求简单正则表达式字符串替换~~~

scbb 2012-10-16 09:11:42
例如:
myBean[3].student[1].score[5].value

替换结果
myBean[4].student[2].score[6].value

其实就是把上面的[]里的数字+1替换。

原因我在使用
http://commons.apache.org/jxpath/
但里面index是开始是1,不是0.
...全文
94 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
oO临时工Oo 2012-10-16
  • 打赏
  • 举报
回复
简单的正则表达式替换貌似不可以。

坐等高手
Ade子夜 2012-10-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

这个试试

String str = "myBean[3].student[1].score[5].value";
Pattern pattern = Pattern.compile("\\[(\\d+)\\]");
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while(……
[/Quote]
ding
adobe2000 2012-10-16
  • 打赏
  • 举报
回复
定义三个变量值,变量+1,就可以达到这种效果了
wapigzhu 2012-10-16
  • 打赏
  • 举报
回复
这个试试

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());
wapigzhu 2012-10-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
Java code

public static void main(String[] args) {
String str = "myBean[3].student[1].score[5].value";
Pattern pattern = Pattern.compile("\\[(\\d+)\\]");
Matcher m = patt……
[/Quote]
这个有问题吧,
str = "myBean[3].student[4].score[5].value"
你看什么结果
安特矮油 2012-10-16
  • 打赏
  • 举报
回复

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);
}

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧