我正在 Kotlin 中使用 Regex 编写一种方法,检查字符串是否包含一个或多个特定代词(例如“I”、“we”、“you”等)。例如,“我们是一家科技公司”应该是匹配的,“网络是为蜘蛛准备的”不应该是匹配的。
我尝试使用以下代码:
fun main() {
val text = "We are testing!"
val regex = "/\b(i|you|we)\b/g".toRegex()
if (regex.containsMatchIn(text.lowercase())) {
println("match")
} else {
println("no match")
}
}
Run Code Online (Sandbox Code Playgroud)
,但它打印“不匹配”。