Kotlin String.split,当分隔符在引号内时忽略

时间:2018-07-16 07:03:03

标签: string split kotlin

我有一个字符串:

Hi there, "Bananas are, by nature, evil.", Hey there.

我想用逗号分隔字符串作为分隔符。如何获取.split方法以忽略引号内的逗号,以便它返回3个字符串而不是5个字符串。

4 个答案:

答案 0 :(得分:3)

您可以在拆分方法中使用regex

根据this answer,以下正则表达式仅匹配,标记之外的"

  

,(?=(?:[^ \“] \” [^ \“] \”) [^ \“] $)

因此请尝试以下代码:

str.split(",(?=(?:[^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*\$)".toRegex())

答案 1 :(得分:2)

您可以使用接受正则表达式的拆分重载:

val text = """Hi there, "Bananas are, by nature, evil.", Hey there."""
val matchCommaNotInQuotes = Regex("""\,(?=([^"]*"[^"]*")*[^"]*$)""")
println(text.split(matchCommaNotInQuotes))

会打印:

[Hi there,  "Bananas are, by nature, evil.",  Hey there.]

考虑阅读this answer,了解在这种情况下正则表达式的工作原理。

答案 2 :(得分:2)

您必须使用能够处理带引号的值的正则表达式。参见Java: splitting a comma-separated string but ignoring commas in quotesC#, regular expressions : how to parse comma-separated values, where some values might be quoted strings themselves containing commas

以下代码显示了此类正则表达式的非常简单的版本。

fun main(args: Array<String>) {

    "Hi there, \"Bananas are, by nature, evil.\", Hey there."
            .split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)".toRegex())
            .forEach { println("> $it") }

}

输出

> Hi there
>  "Bananas are, by nature, evil."
>  Hey there.

请注意正则表达式回溯问题:https://www.regular-expressions.info/catastrophic.html。您最好编写一个解析器。

答案 3 :(得分:2)

如果您不希望使用正则表达式:

val s = "Hi there, \"Bananas are, by nature, evil.\", Hey there."
val hold = s.substringAfter("\"").substringBefore("\"")
val temp = s.split("\"")
val splitted: MutableList<String> = (temp[0] + "\"" + temp[2]).split(",").toMutableList()
splitted[1] = "\"" + hold + "\""

splitted是您想要的列表