David Vega
05/24/2023, 10:37 AMRegex
in Kotlin multiplatform? Or said in other words, how can I know if a String
could potentially match the Regex
? similar to what can be accomplished with java.util.regex.Matcher.hitEnd()
As said, what I’m doing right now with java.util.regex.Matcher
for Kotlin/JVM target is:
fun Regex.partialMatch(input: String): Boolean {
val matcher: Matcher = toPattern().matcher(input)
return if (matcher.matches()) { true } else { matcher.hitEnd() }
}
But I have tried several things and couldn’t find any way of of doing it on KMP.
For example: given this Regex
of a date format (like ‘mm/dd/yyyy’ or ‘dd/mm/yyyy’):
val regex = Regex("[0-9]{2}/[0-9]{2}/[0-9]{4}")
regex.partialMatch("01") // true
regex.partialMatch("01/02") // true
regex.partialMatch("01/02/20") // true
regex.partialMatch("01/02/2023") // true
regex.partialMatch("000") // false
Any ideas?Adam S
05/24/2023, 11:22 AMregex.containsMatchIn("01")
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/contains-match-in.html
edit: oh, I looked properly at your examples now, you want some sort of String.startsWith(regex)
?David Vega
05/24/2023, 11:29 AMAdam S
05/24/2023, 11:32 AM