如何提取从字符串值使用正则表达式
问题描述:
我有以下字符串:如何提取从字符串值使用正则表达式
XYZ,132917057937901,150617,051244,+12.345555,+73.179481,0,153,45,11,1,3,000,00,0.0,9.23,7.40,24.74,0.0,0,0.90,0,0,345,1374,108
现在我想在第16位9.23
和第17位7.40
提取价值。
这个字符串与他们的位置是固定的。
我如何在第16和17位获得价值?
答
使用String.split
代替正则表达式:
String[] split = input.split(',');
String pos16 = split[15];
如果你想使用这种使用正则表达式,比赛并获得组1和2:
Matcher m = Pattern.compile("(?:[^,]*,){15}([^,]*),([^,]*)").matcher(input);
m.find();
String pos16 = m.group(1);
由于它的工作! – deepak
正则表达式不匹配。在{15}之后移除',''。 – saka1029