fun main(args: Array<String>) {
val text = " "id": "5jaq2", "mood" "id": "RKlvj", "is_verified" "id": "XPyZj", "mood""
val regex = Regex("id": (.*?)[,]")
val matches = regex.findAll(text)
val names = matches.map { it.groupValues[1] }.toList()
println(names)
}
我想找到所有的id,但是如果";is_ verified";是在id之后,我不想把它放在列表中。
当前结果:["5jaq2","RKlvj","XPyZj"]
预期:["5jaq2"、"XPyZj"]
知道如何正确地做到这一点吗?
尝试使用:
val regex = Regex("(?:"id":\s)(["\w\d]*?)(?:,\s)(?!"is_verified")")
说明:
(?:"id":\s) // Non-capturing group to match {"id": }
(["\w\d]*?) // Capturing group to catch only words, digits and quotation marks characters
(?:,\s) // Non-capturing group to match {, }
(?!"is_verified") // Negative lookahead to not match if the next letters are {"is_verified"}
测试:https://regexr.com/6inod