Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex doesn't match in Kotlin

Tags:

regex

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")
like image 597
arslancharyev31 Avatar asked Oct 24 '25 20:10

arslancharyev31


1 Answers

You want to match an entire input with matchEntire and a \d+ pattern:

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.

However, A123B does not only consist of digits. If you need to find a partial match, use find.

like image 71
Wiktor Stribiżew Avatar answered Oct 26 '25 10:10

Wiktor Stribiżew