我想用(ex:|)分隔符分割字符串。在这里,我的问题是包含转义字符的字符串。我该如何删除esacape字符内的定界符。
数据如下:
空| 123456 | xxx12345 | 123 | -11234 | 123 | 2000-01-01 | XXX | 01 | 0.000000000000 | 0.000000000000 | 0.000000000000 |“ AAA | AAA数据组(AAA Inc)” |空| 2000-01- 01 | null | null | xx
val delimit='|'
val inputData = 'null|123456|xxx12345|123|-11234|123|2000-01-01|XXX|01|0.000000000000|0.000000000000|0.000000000000|"AAA |AAA Data Group (AAA Inc)"|null|-|2000-01-01|-|null|null|xx'
inputData.split(delimit).map(x=>{println(x)})
我希望得到结果:
null
123456
xxx12345
123
-11234
123
2000-01-01
XXX
01
0.000000000000
0.000000000000
0.000000000000
"AAA AAA Data Group (AAA Inc)"
null
2000-01-01
null
null
xx
但实际输出是:\ n
null
123456
xxx12345
123
-11234
123
2000-01-01
XXX
01
0.000000000000
0.000000000000
0.000000000000
"AAA
AAA Data Group (AAA Inc)"
null
2000-01-01
null
null
xx
答案 0 :(得分:0)
split
接受正则表达式,因此只有在|
不在引号内时,才可以使用正则表达式进行分割:
val inputData = """null|123456|xxx12345|123|-11234|123|2000-01-01|XXX|01|0.000000000000|0.000000000000|0.000000000000|"AAA |AAA Data Group (AAA Inc)"|null|-|2000-01-01|-|null|null|xx"""
inputData.split("\|(?=([^"]*"[^"]*")*[^"]*$)")
// Array(null, 123456, xxx12345, 123, -11234, 123, 2000-01-01, XXX, 01, 0.000000000000, 0.000000000000, 0.000000000000, "AAA |AAA Data Group (AAA Inc)", null, -, 2000-01-01, -, null, null, xx)
我从此question借来了正则表达式。