我有一个Scala代码的问题,我发现这个分裂线.之前我只使用过分割线:
var newLine = line.split(",")
Run Code Online (Sandbox Code Playgroud)
但这种分裂意味着什么呢?
var newLine2 = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)")
Run Code Online (Sandbox Code Playgroud)
我需要拆分的行看起来像这样:
1966, "Green, Green Grass of Home", Tom Jones, 850000
Run Code Online (Sandbox Code Playgroud)
提前致谢!
ste*_*ema 11
split内部的字符串方法定义了一个正则表达式.该组(?=([^\"]*\"[^\"]*\")*[^\"]*$)是一个积极的前瞻性断言.这意味着拆分逗号,但前提([^\"]*\"[^\"]*\")*[^\"]*$是模式跟在逗号之后.
([^\"]* # a series of non double quote characters
\" # a double quote
[^\"]* # a series of non double quote characters
\") # a double quote
* # repeat that whole group 0 or more times
[^\"]*$ # a series of non double quote characters till the end of the string
Run Code Online (Sandbox Code Playgroud)
这意味着当逗号后面有等量的双引号时,它只会在逗号上拆分,换句话说,只有当逗号不在双引号内时才能拆分.(只要字符串中只有引号对,这将有效.)