[英]Matcher java doesn't work but regex seems to be good
I want to find instance of regex in my string. 我想在我的字符串中找到正则表达式的实例。 But it doesn't work.
但它不起作用。 My regex seems to be good.
我的正则表达式似乎很好。
My string is like that : 我的字符串是这样的:
LB,32736,0,T,NRJ.POMPES_BACHE.PUISSANCE_ELEC_INST,20190811T080000.000Z,20190811T194400.000Z
TR,NRJ.POMPES_BACHE.PUISSANCE_ELEC_INST,0,65535,1,1,,0,0,2
20190811T080000.000Z,0.00800000037997961,192
20190811T080100.000Z,0.008999999612569809,192
20190811T080200.000Z,0.008999999612569809,192
LB,32734,0,T,NRJ.POMPES_BACHE.PUISSANCE_ELEC_CPT,20190811T080000.000Z,20190811T201200.000Z
TR,NRJ.POMPES_BACHE.PUISSANCE_ELEC_CPT,0,65535,1,1,,0,0,2
20190811T080000.000Z,0.6743068099021912,192
20190811T080100.000Z,0.6744459867477417,192
20190811T080200.000Z,0.6745882630348206,192
20190811T080300.000Z,0.6747232675552368,192
20190811T080400.000Z,0.6748600006103516,192
20190811T080500.000Z,0.6749916672706604,192
20190811T080600.000Z,0.6751362681388855,192
And I want to match only lines which have this format 我想只匹配具有此格式的行
20190811T080000.000Z,0.00800000037997961,192
So I have tried this regex 所以我试过这个正则表达式
^([^,]*,){2}[^,]*$
And work on this website : https://regex101.com/r/iIbpgB/3 并在此网站上工作: https : //regex101.com/r/iIbpgB/3
But, when I implement it on Java, it doesn't work. 但是,当我在Java上实现它时,它不起作用。
Pattern pattern = Pattern.compile("^([^,]*,){2}[^,]*$");
Matcher matcher = pattern.matcher(content);
if ( matcher.find()){
System.out.println(matcher.group());
}
You can verify here : https://www.codiva.io/p/e83bcde1-8528-4330-94a2-58fe80afffc0 你可以在这里验证: https : //www.codiva.io/p/e83bcde1-8528-4330-94a2-58fe80afffc0
Someone have an explain?.. Thanks 有人有解释吗?谢谢
Your are missing MULTILINE
mode in your Java regex, you may use: 您在Java正则表达式中缺少
MULTILINE
模式,您可以使用:
Pattern pattern = Pattern.compile("^([^,]*,){2}[^,]*$", Pattern.MULTILINE);
or else use inline: 或者使用内联:
Pattern pattern = Pattern.compile("(?m)^([^,]*,){2}[^,]*$");
You changed between if and while, do you want 1 match or all of them? 你在if和while之间改变了,你想要1场比赛还是全场比赛?
Pattern pattern = Pattern.compile("^([^,]*,){2}[^,]*$");
Matcher matcher = pattern.matcher(content);
while ( matcher.find()){
System.out.println(matcher.group());
}
This version will keep looping while the matcher continues to match the regex in the content string. 当匹配器继续匹配内容字符串中的正则表达式时,此版本将保持循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.