寻求合适的正则表达式到达需求,高手请进!谢谢

apinglee 2009-04-08 05:15:46
有一串类似如下:
String strT = "round-trip (ms) min/avg/max = 0.956/5/6.752";

由于限制,现在需要三个正则表达式予以获取串中的数据。
比如,可以用"ms\\D*(\\d+\\.?\\d*)"匹配获取第一个数据0.021


类似请问第二个、第三个正则表达式咋个写?

尽量简单可行。
...全文
124 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
baobao04551 2009-04-10
  • 打赏
  • 举报
回复
4楼正解!
apinglee 2009-04-09
  • 打赏
  • 举报
回复
写错了,应该是0.956
jonay 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 apinglee 的帖子:]
有一串类似如下:
String strT = "round-trip (ms) min/avg/max = 0.956/5/6.752";

由于限制,现在需要三个正则表达式予以获取串中的数据。
比如,可以用"ms\\D*(\\d+\\.?\\d*)"匹配获取第一个数据0.021


类似请问第二个、第三个正则表达式咋个写?

尽量简单可行。
[/Quote]
你的0.021 从哪来的,晕死!!!!!!!!!!!!!!!!
hbgzg3006 2009-04-09
  • 打赏
  • 举报
回复
String s="round-trip (ms)  min/avg/max = 0.956/5/6.752";
String regex="\\d+\\.?\\d*";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(s);
while(m.find()){
System.out.println(m.group());
}
jonay 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 apinglee 的回复:]
用"ms\\D*(\\d+\\.?\\d*)"匹配获取第一个数据0.021
[/Quote]
真的吗?我怎么试的结果是 ms) min/avg/max = 0.956

楼主,你不能认真写一下吗,你想输出的是
0.956
5
6.752
这三个数吗?如果是的话

public static void main(String[] args) {

String strT = "round-trip (ms) min/avg/max = 0.956/5/6.752";
String[] regex = new String[]{"\\d\\.?\\d*(?=/.*/)","(?<=/)\\d\\.?\\d*(?=/)","\\d\\.?\\d*$"};

Pattern p = null;
Matcher m = null;
for (int i =0;i < 3; i++) {
p = Pattern.compile(regex[i]);
m = p.matcher(strT);
while (m.find()) {
System.out.print(m.group());
}
System.out.println();
}

}

输出
0.956
5
6.752
swoky 2009-04-08
  • 打赏
  • 举报
回复
没弄懂什么意思,可否说得详细点

62,612

社区成员

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

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