Suppose you have a string http://server.com/s123456 and want to get 123456 from it. Or http://server.com/s123456&a=100 and get 123456, 100. Many different situations where you want to have parts of a string.
In order to match strings I use regular expressions.
@Test
fun test() {
val text = "http://server.com/s123456" // Source string.
val reg = "^http://server.com/s\\d+\$".toRegex()
assertEquals(true, reg.matches(text))
// Try to split the string with findAll().
val a: Sequence<MatchResult> = reg.findAll(text, 0)
val names = a.map { it.groupValues[0] }.joinToString()
println(names) // http://server.com/s123456
}
But it doesn't split the source string into chars and digits, just prints: http://server.com/s123456. Is there a way to write a mask, so that we can retrieve numbers (or any other parts) from the string?
"^http://server.com/s(\\d+)\$"try this and use 1 ingroupValuesindex.