In this article, php editor Xiaoxin will introduce you to regular expressions with nested repetitions. Regular expressions are a powerful string matching tool that can be used to find, replace, and validate strings. Nested repetition refers to using one or more repeated patterns in a regular expression, and these repeated patterns can be nested in other patterns. This technique can be used to match text with a hierarchical structure, such as HTML tags, nested brackets, etc. By understanding the syntax and usage of nested repeated regular expressions, we can handle various complex string matching needs more flexibly.
I'm trying to create a regular expression in go that matches up to 50 words separated by spaces, where each word is 1-32 "a" I'm using the following regular expression
regexp.compile(`^(a{1,32}\s?){1,50}$`)
I get the following error
error parsing regexp: invalid repeat count: `{1,50}`
I noticed that it can be repeated up to 31 times, like this
r, err := regexp.Compile(`^(a{1,32}\s?){1,31}$`)
See https://go.dev/play/p/rlnrox9-57_m
Go's regexp
engine has a limit Where the combination of the top layer and any inner repeats must not exceed 1000 copies of the innermost repeat part. This is documented in the re2 Grammar Specification.
In your case, up to 31 is valid since inner 32 * outer 31 = 992. 32 * 32 = 1024 and 32 * 50 = 1600 beyond that limit will not work.
The solution is to split the expression into multiple parts: ^(a{1,32}\s?){1,31}(a{1,32}\s?){0, 19}$
p>
The above is the detailed content of Regular expression with nested repetitions. For more information, please follow other related articles on the PHP Chinese website!