Java/Kotlin:标记字符串忽略嵌套引号的内容

我想用空格分割一个字符,但将空格保留在引号内(以及引号本身)。问题是,引号可以嵌套,而且我需要对单引号和双引号都这样做。所以,从this "'"is a possible option"'" and ""so is this"" and '''this one too''' and even ""mismatched quotes"我想得到的行[this, "'"is a possible option"'", and, ""so is this"", and, '''this one too''', and, even, ""mismatched quotes"]

这个问题已经被问过了,但不是我要问的确切问题。这里有几个解决方案:一个使用匹配器(在这种情况下"""x"""将被拆分为[""", x"""],所以这不是我需要的)和 Apache Commons (它可以使用"""x"""但不能使用""x"",因为它需要前两个双引号并留下最后两个与x)。也有人建议手动编写一个函数,但这是最后的手段。


慕桂英546537
浏览 239回答 1
1回答

森林海

您可以使用以下正则表达式来实现:["']+[^"']+?["']+. 使用该模式,您可以检索要拆分的索引,如下所示:val indices = Regex(pattern).findAll(this).map{ listOf(it.range.start, it.range.endInclusive) }.flatten().toMutableList()剩下的就是用子字符串构建列表。这里完整的功能:fun String.splitByPattern(pattern: String): List<String> {&nbsp; &nbsp; val indices = Regex(pattern).findAll(this).map{ listOf(it.range.start, it.range.endInclusive) }.flatten().toMutableList()&nbsp; &nbsp; var lastIndex = 0&nbsp; &nbsp; return indices.mapIndexed { i, ele ->&nbsp; &nbsp; &nbsp; &nbsp; val end = if(i % 2 == 0) ele else ele + 1 // magic&nbsp; &nbsp; &nbsp; &nbsp; substring(lastIndex, end).apply {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastIndex = end&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}用法:val str = """this "'"is a possible option"'" and ""so is this"" and '''this one too''' and even ""mismatched quotes"""".trim()println(str.splitByPattern("""["']+[^"']+?["']+"""))输出:[this , "'" is a possible option"'", and , ""so is this"", and , '''this one too''', and even , ""mismatched quotes"]在Kotlin 的操场上试试吧!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java