Aller au contenu

Regex Recompiled In Loop (ARCH8073)

Ce contenu n’est pas encore disponible dans votre langue.

Key kotlin.performance.regex-recompiled-in-loop · Severity warning · Category performance

Flags Regex(...) / toRegex() / Pattern.compile inside a loop body - the pattern is compiled on every iteration.

for (line in lines) {
if (Regex("\\d+").matches(line)) count++
}
val digits = Regex("\\d+")
for (line in lines) {
if (digits.matches(line)) count++
}

Hoist the Regex to a property or a local outside the loop.