简体   繁体   English

C ++ RegEx重复捕获组

[英]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) . {{name1 | filter1|filter2 |filter3}}放入(name1, filter1, filter2, filter3)

I have a RegEx: 我有一个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 (...)*. 我想找到第二组的所有事件 ,这些事件都标有一颗Kleene星(...)*。 The problem is that I can only find last occurrence of the group. 问题是我只能找到该组的最后一次出现。

Instead I use the following RegEx: 相反,我使用以下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. 查找第二个捕获组(整个子字符串" | filter1|filter2 |filter3" ),然后使用另一个RegEx对其进行解析。

How can it be done in C++? 如何在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 | 在此,()组匹配以下1个实例:SP | SP WORD Even though the "*" matches zero or more instances of that. SP WORD即使“ *”匹配零个或多个实例。 Change it to: 更改为:

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

Or, to be clear that the inner () isn't a tagged expression: 或者,要明确说明inner()不是带标记的表达式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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