nkiesel
04/29/2021, 8:04 PMfor (line in lines) {
if (Regex("#(.) A (.+)").matches(line)) {
val m = Regex("#(.) A (.+)").matchEntire(line)
...
} else if (Regex("#(.) B (.+)").matches(line)) {
val m = Regex("#(.) B (.+)").matchEntire(line)
...
}
}
(real code of course has regexes defined only once etc.). I now changed it to
for (line in lines) {
val m1 = Regex("#(.) A (.+)").matchEntire(line)
if (m1 != null) {
...
continue
}
val m2 = Regex("#(.) B (.+)").matchEntire(line)
if (m2 != null) {
...
continue
}
}
but that does not look too nice (esp. the need for repeated continue
)nanodeath
04/29/2021, 8:05 PMnanodeath
04/29/2021, 8:06 PMmatchEntire
already returns null if there's no matchnanodeath
04/29/2021, 8:07 PMnkiesel
04/29/2021, 8:09 PMnkiesel
04/29/2021, 8:14 PMwhen
which understands regex and injects the MatchResult into the block
when (line) {
Regex("A(.+)") -> println(it.groupValues[1])
Regex("B(.)(.+)") -> println(it.groupValues[2])
}
(i.e. here it
would refer to the MatchResult)nanodeath
04/29/2021, 8:19 PMnanodeath
04/29/2021, 8:20 PMsequenceOf({ performMatch }).firstNotNull()
trickery you could do but I don't think it's worth itNir
04/29/2021, 8:21 PMNir
04/29/2021, 8:23 PMfirstMatchEntire(line,
Regex("#(.) A (.+)") to { .... },
Regex(...) to { ... }
)
Nir
04/29/2021, 8:23 PMnanodeath
04/29/2021, 8:24 PMephemient
04/29/2021, 8:29 PMregex1.matchEntire(line)?.also { match ->
} ?: regex2.matchEntire(line)?.also { match ->
} ?: regex3.matchEntire(line)?.also { match ->
} // etc.
ephemient
04/29/2021, 8:32 PM