我想在我的字符串中找到正则表达式的实例。但它不起作用。我的正则表达式似乎不错。我的字符串是这样的:LB,32736,0,T,NRJ.POMPES_BACHE.PUISSANCE_ELEC_INST,20190811T080000.000Z,20190811T194400.000ZTR,NRJ.POMPES_BACHE.PUISSANCE_ELEC_INST,0,65535,1,1,,0,0,220190811T080000.000Z,0.00800000037997961,19220190811T080100.000Z,0.008999999612569809,19220190811T080200.000Z,0.008999999612569809,192LB,32734,0,T,NRJ.POMPES_BACHE.PUISSANCE_ELEC_CPT,20190811T080000.000Z,20190811T201200.000ZTR,NRJ.POMPES_BACHE.PUISSANCE_ELEC_CPT,0,65535,1,1,,0,0,220190811T080000.000Z,0.6743068099021912,19220190811T080100.000Z,0.6744459867477417,19220190811T080200.000Z,0.6745882630348206,19220190811T080300.000Z,0.6747232675552368,19220190811T080400.000Z,0.6748600006103516,19220190811T080500.000Z,0.6749916672706604,19220190811T080600.000Z,0.6751362681388855,192我只想匹配具有这种格式的行20190811T080000.000Z,0.00800000037997961,192所以我试过这个正则表达式^([^,]*,){2}[^,]*$并在此网站上工作:https ://regex101.com/r/iIbpgB/3但是,当我在 Java 上实现它时,它不起作用。Pattern pattern = Pattern.compile("^([^,]*,){2}[^,]*$"); Matcher matcher = pattern.matcher(content); if ( matcher.find()){ System.out.println(matcher.group()); }您可以在这里验证:https://www.codiva.io/p/e83bcde1-8528-4330-94a2-58fe80afffc0有人有解释吗?..谢谢
2 回答
繁星淼淼
TA贡献1775条经验 获得超11个赞
您的 Java 正则表达式中缺少MULTILINE
模式,您可以使用:
Pattern pattern = Pattern.compile("^([^,]*,){2}[^,]*$", Pattern.MULTILINE);
或者使用内联:
Pattern pattern = Pattern.compile("(?m)^([^,]*,){2}[^,]*$");
白板的微信
TA贡献1883条经验 获得超3个赞
您在 if 和 while 之间进行了更改,您想要 1 场比赛还是全部比赛?
Pattern pattern = Pattern.compile("^([^,]*,){2}[^,]*$");
Matcher matcher = pattern.matcher(content);
while ( matcher.find()){
System.out.println(matcher.group());
}
当匹配器继续匹配内容字符串中的正则表达式时,此版本将继续循环。
添加回答
举报
0/150
提交
取消