我试图区分单词和字符串。我设法让字符串工作,但我无法弄清楚如何只匹配没有双引号括起来的单词:
所以我想要匹配:
test
但这不应该匹配:
"test"
这是我到目前为止所做的:
[^\"][a-zA-Z]*[^\"]
尽管它被双引号包围,但仍然可以进行测试。
Input: "\"this is a string\" word"
Expected Output: word
有什么建议吗?
答案 0 :(得分:1)
怎么样?
assert("\"<quoted>\" word".words == listOf("word"))
assert("head \"<quoted>\" word".words == listOf("head", "word"))
assert("head\"<quoted>\"word".words == listOf("head", "word"))
assert("\"<escaped\\\"quoted>\"".words == emptyList())
assert("; punctuations , ".words == listOf("punctuations"))
inline val String.words get() = dropStrings().split("[^\\p{Alpha}]+".toRegex())
.filter { it.isNotBlank() }
@Suppress("NOTHING_TO_INLINE")
inline fun String.dropStrings() = replace("\"(\\[\"]|.*)?\"".toRegex(), " ")