hello all! can i add var like pattern in Regex? so...
# getting-started
c
hello all! can i add var like pattern in Regex? something like
var regWord = word.toRegex
var list = line.split(Regex("""^(regWord)+"""))
i need to count regWord in line
k
Can you elaborate a bit? Can you give some example input/output?
c
I apologize for the Russian characters, I will try to change it i have
list
, it stores `word`s, the number of which I need to count in a line. I select one of the `word`s (it may be part of it as "DIFFERENT" and "ent") and after using
split
I delete everything except these `word`s. then I count their number. maybe this can be done through
find
I do not have
text
or `line`s on input, they are randomly generated
mapOf("DIFFERENT" to 2, "ent" to 2, "Sloppy" to 1, "z" to 49, "evolution" to 0)
k
So you want to cunt how often each word in a list of words occurs in a string?
c
yep
k
I can't really find anything convenient to count that, something like
Copy code
val string = "abcbcc"
val words = listOf("a", "b", "c")
val result = words.associateWith { word ->
    generateSequence(0) { i -> string.indexOf(word, startIndex = i + 1).takeIf { it != -1 } }.count() - 1
}
println(result)
works though.