gpt4 book ai didi

Java : Splitting a String using Regex

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:48:14 24 4
gpt4 key购买 nike

我必须使用逗号 (,) 作为分隔符来拆分字符串并忽略引号 (") 内的任何逗号

字段分隔符:,
字段分组器:“

要拆分的字符串是:"1","2",3,"4,5"

我可以按如下方式实现它:

String record = "\"1\",\"2\",3,\"4,5\"";
String[] tokens = record.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");

输出:

"1"
"2"
3
"4,5"

现在的挑战是 fieldGrouper(") 不应该是分割标记的一部分。我无法为此找出正则表达式。

拆分的预期输出是:

1
2
3
4,5

最佳答案

更新:

String[] tokens = record.split( "(,*\",*\"*)");

结果:
Image Link

Initial Solution:
( doesn't work @ .split method )

This RexEx pattern will isolate the sections you want:
(?:\\")(.*?)(?:\\")

It uses non-capturing groups to isolate the pairs of escaped quotes, and a capturing group to isolate everything in between.

Check it out here: Live Demo

关于Java : Splitting a String using Regex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35842895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com