I would like help to create an input masking using find and replace regexes for the following use case:
The user types 11 digits of his identification number and the output will be formatted with this pattern:
\d\d\d.\d\d\d.\d\d\d-\d\d
First attempt
Find: (\d{3})(\d{3})(\d{3})(\d{2})
Replace: $1.$2.$3-$4
This only works after the user types all the 11 digits. However, I want the dots and dashes to appear while the user types.
Second attempt
Find: (\d{1,3})(\d{1,3})(\d{1,3})(\d{1,2})
Replace: $1.$2.$3-$4
As soon as the user types the fourth digit, but the result ends up being like this \d.\d.\d-\d
Third attempt
Find: (\d{3})(\d{0,3})(\d{0,3})(\d{0,2})
Replace: $1.$2.$3-$4
As soon as the user types the third digit, but the result ends up being like this \d\d\d\..-
Code:
A brief description of the code:
fun transformation(input: String, findRegex: String, replaceRegex: String): String =
input.replace(findRegex.toRegex(), replaceRegex)
fun main() {
val input = "01121212"
val findRegex = """(\d{3})(\d{3})(\d{3})(\d{2})"""
val replaceRegex = """$1.$2.$3-$4"""
val result = transformation(input, findRegex, replaceRegex)
println(result)
}
Full code can be found here: https://gitlab.com/pertence/masked-textinput