简体   繁体   中英

C++ RegEx repeated capturing groups

I am trying to parse strings of the form

{{name1 | filter1|filter2 |filter3}} {{name1 | filter1|filter2 |filter3}} into (name1, filter1, filter2, filter3) .

I have a RegEx:

static const regex r("\\{\\{\\s*([\\.\\w]+)(\\s*\\|\\s*[\\.\\w]+)*\\s*\\}\\}");

And I want to find all occurences of the second group, which marked with a Kleene star (...)*. The problem is that I can only find last occurrence of the group.

Instead I use the following RegEx:

static const regex r("\\{\\{\\s*([\\.\\w]+)((\\s*\\|\\s*[\\.\\w]+)*)\\s*\\}\\}");

To find the second capture group (whole substring " | filter1|filter2 |filter3" ) and parse it with another RegEx.

How can it be done in C++?

The most similar question is here: Regex: Repeated capturing groups

You need to add () around the "*" expression meant to match the second group.

(\s*\|\s*[\.\w]+)*

Here, the () group matches 1 instance of: SP | SP WORD Even though the "*" matches zero or more instances of that. Change it to:

((\s*\|\s*[\.\w]+)*)

Or, to be clear that the inner () isn't a tagged expression:

((?n:\s*\|\s*[\.\w]+)*)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM