假设我具有如下单个字符串内容
chart: {
alignTicks: false
}
基本上,当定界符'带有问号时,我想将其视为同一行。行定界符是'
如何在Java中标记化片段并获得计数?
谢谢。
答案 0 :(得分:1)
您可以在正则表达式中使用否定式后退:
String input = "FTX+AAA+++201707141009UTC'\n"
+ " FTX+BBB+++201707141009UTC'\n"
+ " FTX+CCC+++201707141009UTC?:??'\n"
+ " PISCO US LTS;?:V.D??' \n"
+ " SOUZA?:GB?:GB'\n"
+ " FTX+ZZZ+++201707141009UTC'";
String[] tokens = input.split("(?<!\\?)'\\s*");
System.out.println(tokens.length);
4
但是,在第二个示例中,我希望有两个细分市场,而不是三个...
答案 1 :(得分:0)
上述内容的另一种选择-但再次表明您发布的第二个示例可能是错误的,因为第三行以App\Http\Middleware
结尾,根据您的定义,这不应中断。
?'
打印
4-> [FTX + AAA +++ 201707141009UTC,FTX + BBB +++ 201707141009UTC,FTX + CCC +++ 201707141009UTC?:??'PISCO US LTS;?:VD ??'SOUZA?:GB? :GB,FTX + ZZZ +++ 201707141009UTC]
2-> [FTX + AAA +++ 201707141009UTC,FTX + CCC +++ 201707141009UTC?:??'PISCO US LTS;?:V.D ??'FTX + ZZZ +++ 201707141009UTC]
答案 2 :(得分:0)
我认为他/她想要的是这个东西
String a = "FTX+AAA+++201707141009UTC'"
+ "FTX+BBB+++201707141009UTC'"
+ "FTX+CCC+++201707141009UTC?:??'"
+ "PISCO US LTS;?:V.D??' "
+ "SOUZA?:GB?:GB'"
+ "FTX+ZZZ+++201707141009UTC'";
String result[] = a.split("'");
List<String> stringList = new ArrayList<String>(Arrays.asList(result));
for (int i = 0; i < stringList.size(); i++) {
if (!stringList.get(i).startsWith("FTX") && i != 0) {
stringList.set(i-1, stringList.get(i-1) + stringList.get(i));
stringList.remove(i);
i--;
}
}
for (int j = 0; j < stringList.size(); j++) {
System.out.println(stringList.get(j));
}
FTX + AAA +++ 201707141009UTC
FTX + BBB +++ 201707141009UTC
FTX + CCC +++ 201707141009UTC?:?? PISCO US LTS;?:V.D ?? SOUZA?:GB?:GB
FTX + ZZZ +++ 201707141009UTC