我想使用正则表达式分割文本。 例如,我有字符串:
1,2,3,4,5,6,7
1,2,3,4,5,7,7,8,9,10
1,2,3
1,2,3,4,5,6,7
1,2,3,4,5,7,7,8,9,10
1,2,3
我想这样分割我的字符串:
数组的第一个单元格:
1,2,3,4,5,6,7
1,2,3,4,5,7,7,8,9,10
1,2,3
第二个数组单元格
1,2,3,4,5,6,7
1,2,3,4,5,7,7,8,9,10
1,2,3
为此,我要使用正则表达式。
我的分隔符是我的前2行
所以我已经尝试过以下正则表达式:
(.*,){6}.*\s(.*,){9}.*
但这对我不起作用...而且不明白为什么。
如果有人可以给我解释一下。
在拆分之后,我想将定界符保留在数组中。因为分隔符将在分割后删除。
谢谢
编辑:
我将向您展示我的应用程序(我已经通过数字恢复了它,但是更复杂了),并且我有一个像这样的字符串:
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
我想拥有 在第一个单元格中:
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
也许使用Java库的Regex Matcher / Pattern比分割功能更好
答案 0 :(得分:0)
此解决方案是插入定界符并利用Pattern的分割。 尝试一下:
public static void main(String[] args) {
String input = "LB,32736,0,T,NRJ.POMPES_BACHE.PUISSANCE_ELEC_INST,20190811T080000.000Z,20190811T194400.000Z\n"
+ "TR,NRJ.POMPES_BACHE.PUISSANCE_ELEC_INST,0,65535,1,1,,0,0,2\n"
+ "20190811T080000.000Z,0.00800000037997961,192\n"
+ "20190811T080100.000Z,0.008999999612569809,192\n"
+ "20190811T080200.000Z,0.008999999612569809,192\n"
+ "LB,32734,0,T,NRJ.POMPES_BACHE.PUISSANCE_ELEC_CPT,20190811T080000.000Z,20190811T201200.000Z\n"
+ "TR,NRJ.POMPES_BACHE.PUISSANCE_ELEC_CPT,0,65535,1,1,,0,0,2\n"
+ "20190811T080000.000Z,0.6743068099021912,192\n"
+ "20190811T080100.000Z,0.6744459867477417,192\n"
+ "20190811T080200.000Z,0.6745882630348206,192\n"
+ "20190811T080300.000Z,0.6747232675552368,192\n"
+ "20190811T080400.000Z,0.6748600006103516,192\n"
+ "20190811T080500.000Z,0.6749916672706604,192\n"
+ "20190811T080600.000Z,0.6751362681388855,192\n";
Pattern pattern = Pattern.compile("^(LB,\\d+,)", Pattern.MULTILINE|Pattern.DOTALL);
String stringWithDelimiter = pattern.matcher(input).replaceAll("#$1")
.replaceFirst("#", "");
List<String> cells = Arrays.stream(Pattern.compile("#").split(stringWithDelimiter))
.collect(Collectors.toList());
cells.forEach(s -> System.out.println("\nCell:\n'" + s + "'"));
}
“ cells.forEach(...):”
的输出Cell:
'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
'
Cell:
'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
'