Skip to content

regexp-match-in-loop

Default severity: warning

The package-level regexp matching helpers compile their pattern on every call. Calling them with a constant pattern inside a loop repeats the same compilation. Compile the expression once before the loop and reuse it.

Dynamic patterns are accepted because hoisting them may change behavior.

for _, value := range values { regexp.MatchString(`^[a-z]+$`, value) }
pattern := regexp.MustCompile(`^[a-z]+$`); for _, value := range values { pattern.MatchString(value) }