ken_kentan
09/14/2020, 9:18 AM"AAA{foo}BBB{bar}CCC"
to ["AAA", "{foo}", "BBB", "{bar}", "CCC"]
if regex is \{.*?}
Tobias Berger
09/14/2020, 9:40 AMfun customSplit(input: String): List<String> {
return "([^{]+)(\\{.*?})?".toRegex().findAll(input).flatMap { match ->
sequence {
yield(match.groupValues[1])
match.groupValues[2].takeIf { it.isNotBlank() }?.let { yield(it) }
}
}.toList()
}
ephemient
09/14/2020, 9:52 AM"AAA{foo}BBB{bar}CCC".split("""(?=\{)|(?<=})""".toRegex())
(not all JS RegExp engines support lookbehind though)Tobias Berger
09/14/2020, 9:55 AM{
and after each }
.ephemient
09/14/2020, 9:56 AM{{{foo}
? yours drops the leading {
, mine separates them)ken_kentan
09/14/2020, 9:59 AMAAA{foo}BBB{bar}CCC}DDD{EEE
etc..Tobias Berger
09/14/2020, 10:00 AMfun customSplit(input: String, regex: Regex): List<String> {
var nextSliceIndex = 0
val result = regex.findAll(input).fold(mutableListOf<String>()) { result, match ->
val range = match.range
result += input.slice(nextSliceIndex until range.first)
result += input.slice(range)
nextSliceIndex = range.last + 1
result
}
if (nextSliceIndex < input.length) {
result += input.substring(nextSliceIndex)
}
return result
}
stephanmg
09/14/2020, 10:02 AMken_kentan
09/14/2020, 10:13 AM