我想在我的字符串中找到正則表達式的實例。但它不起作用。我的正則表達式似乎不錯。我的字符串是這樣的: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
提交
取消