正则表达式在Kotlin中不匹配 [英] Regex doesn't match in Kotlin
本文介绍了正则表达式在Kotlin中不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白为什么这个简单的正则表达式不匹配任何东西.它总是失败并引发异常:
I can't understand why this simple regex doesn't match anything. It always fails and throws exception:
val match = Regex("""\d+""").matchEntire("A123B")?: throw Exception("Regex fail")
推荐答案
You want to match an entire input with matchEntire
and a \d+
pattern:
fun matchEntire(input: CharSequence): MatchResult? (source)
尝试将整个输入CharSequence与该模式进行匹配.
如果整个输入匹配,则返回MatchResult的一个实例;否则返回null.
fun matchEntire(input: CharSequence): MatchResult? (source)
Attempts to match the entire input CharSequence against the pattern.
Return An instance of MatchResult if the entire input matches or null otherwise.
但是,A123B
不仅由数字组成.如果需要查找部分匹配项,请使用
However, A123B
does not only consist of digits. If you need to find a partial match, use find
.
这篇关于正则表达式在Kotlin中不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文