小编ola*_*tre的帖子

在 Kotlin 中使用正则表达式查找字符串中的一个或多个单词

我正在 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)

,但它打印“不匹配”。

regex kotlin

4
推荐指数
1
解决办法
4666
查看次数

标签 统计

kotlin ×1

regex ×1