Regular expression with nested repetitions

王林
Release: 2024-02-08 21:20:30
forward
1185 people have browsed it

Regular expression with nested repetitions

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.

Question content

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}$`)
Copy after login

I get the following error

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

I noticed that it can be repeated up to 31 times, like this

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

See https://go.dev/play/p/rlnrox9-57_m

Solution

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!