Home > Blockchain >  Regex with nested repetition
Regex with nested repetition

Time:01-27

I'm trying to create a regex in Go that matches up to 50 words separated by white space where each word is 1-32 "a"s I'm using the following regex

regexp.Compile(`^(a{1,32}\s?){1,50}$`)

and I am getting the following error

error parsing regexp: invalid repeat count: `{1,50}`

I've noticed that it does work up to 31 repetitions like so

r, err := regexp.Compile(`^(a{1,32}\s?){1,31}$`)

see https://go.dev/play/p/RLnroX9-57_m

CodePudding user response:

Go's regexp engine has a limit where combination of top level and any inner repetitions must not exceed 1000 copies of the innermost repeated part. This is documented in re2 Syntax spec.

In your case up to 31 works because inner 32 * outer 31 = 992. 32 * 32 = 1024 and also 32 * 50 = 1600 won't work for exceeding that limit.

Workaround is to split expression into multiple parts: ^(a{1,32}\s?){1,31}(a{1,32}\s?){0,19}$

  • Related