---
title: "regexp-match-in-loop"
description: "Detect repeated regexp compilation inside loops."
image: "https://strider.gempir.com/og.png"
---

> Documentation Index
> Fetch the complete documentation index at: https://strider.gempir.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 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.

## Bad

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

## Good

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

Source: https://strider.gempir.com/analyzers/regexp-match-in-loop/index.mdx
