我不明白为什么这个简单的正则表达式不匹配任何东西。它总是失败并抛出异常:
val match = Regex("""\d+""").matchEntire("A123B")?: throw Exception("Regex fail")
Run Code Online (Sandbox Code Playgroud)
您想将整个输入与matchEntire一个\d+模式匹配:
fun matchEntire(input: CharSequence): MatchResult? (source)
尝试将整个输入 CharSequence 与模式匹配。
如果整个输入匹配,则返回 MatchResult 的实例,否则返回 null。
然而,A123B不仅仅由数字组成。如果您需要查找部分匹配项,请使用find.