作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我必须使用逗号 (,) 作为分隔符来拆分字符串并忽略引号 (") 内的任何逗号
字段分隔符:,
字段分组器:“
要拆分的字符串是:"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( "(,*\",*\"*)");
结果:
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/
我是一名优秀的程序员,十分优秀!