golang/go

regexp: repeat count limit is lower than documented

Open

#78,222 建立於 2026年3月19日

在 GitHub 查看
 (4 留言) (0 反應) (0 負責人)Go (133,883 star) (19,008 fork)batch import
BugReportDocumentationNeedsInvestigationhelp wanted

描述

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

貢獻者指南