golang/go

regexp: repeat count limit is lower than documented

开放

#78,222 创建于 2026年3月19日

 (4 条评论) (0 个反应) (0 位负责人)Go (19,008 个派生)batch import

仓库指标

星标
 (133,883 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

regexp/syntax tells about a repeat count limit at 1000, but for some cases it is lower (250). Those cases should be documented.

Go version

go version go1.26.1 darwin/arm64

Output of go env in your module/workspace:

N/A

What did you do?

On the Go Playground:

var (
	ok1 = regexp.MustCompile(`(?:[a-z]{4}){0,250}`)
	ok2 = regexp.MustCompile(`(?:[a-z]{4}){0,250}(?:[a-z]{4})?`)
	// Doc of regexp in Go 1.26 says:
	// > Implementation restriction: The counting forms x{n,m}, x{n,}, and x{n} reject
	// > forms that create a minimum or maximum repetition count above 1000.
	// But this is rejected:
	fail = regexp.MustCompile(`(?:[a-z]{4}){0,251}`)
)

What did you see happen?

panic: regexp: Compile(`(?:[a-z]{4}){0,251}`): error parsing regexp: invalid repeat count: `{0,251}`

goroutine 1 [running]:
regexp.MustCompile({0x4b13d6, 0x13})
	/usr/local/go-faketime/src/regexp/regexp.go:313 +0xb4
main.init()
	/tmp/sandbox1168500107/prog.go:12 +0x8d

What did you expect to see?

The documentation of regexp/syntax (Go 1.26.1, latest) says about repetitions:

Implementation restriction: The counting forms x{n,m}, x{n,}, and x{n} reject forms that create a minimum or maximum repetition count above 1000. Unlimited repetitions are not subject to this restriction.

As the example shows, the restriction is lower for some cases, 250 here. Those cases must be better documented (and changes advertised in release notes).

贡献者指南