正则表达式 – 匹配不是字符串的字
我试图区分单词和字符串。 我设法使字符串工作,但我不能完全弄清楚如何匹配不包含双引号的单词:
所以我想这匹配:
test
但是这不应该匹配:
"test"
这是我迄今为止:
[^\"][a-zA-Z]*[^\"]
它仍然得到了测试,虽然它是由双引号包围。
Input: "\"this is a string\" word" Expected Output: word
有什么建议么?
这个怎么样?
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(), " ")