Skip to content

regexp: repeat count limit is lower than documented #78222

@dolmen

Description

@dolmen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.DocumentationIssues describing a change to documentation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.help wanted

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions