正则表达式:如何捕获重复捕获组中的所有迭代
Regex: How to capture all iterations in repeated capturing group
我希望 C# 的这些行:
var regex = new Regex("A(bC*)*");
var match = regex.Match("AbCCbbCbCCCCbbb");
var groups = match.Groups;
到 return 类似于:
["AbCCbbCbCCCCbbb", "A", "bCC", "b", "bC", "bCCC", "b", "b", "b"]
但它 return 只是最后捕获的匹配项:
["AbCCbbCbCCCCbbb", "b"]
Here Regex101 还显示以下内容作为警告:
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
我应该如何更改我的正则表达式模式?
也许试试这个:
A|b(C+)?
测试于 Notepad++
编辑:如果您想要此模式与组:
(A)|(b(C+)?)
如果您还想捕获 A
,只需用括号括起来:new Regex("(A)(bC*)*")
。见 regex demo.
然后,收集你在里面得到的所有值CaptureCollection:
var regex = new Regex("(A)(bC*)*");
var match = regex.Matches("AbCCbbCbCCCCbbb")
.Cast<Match>()
.SelectMany(x => x.Groups.Cast<Group>()
.SelectMany(v => v.Captures
.Cast<Capture>()
.Select(t => t.Value)
)
)
.ToList();
foreach (var s in match)
Console.WriteLine(s);
我希望 C# 的这些行:
var regex = new Regex("A(bC*)*");
var match = regex.Match("AbCCbbCbCCCCbbb");
var groups = match.Groups;
到 return 类似于:
["AbCCbbCbCCCCbbb", "A", "bCC", "b", "bC", "bCCC", "b", "b", "b"]
但它 return 只是最后捕获的匹配项:
["AbCCbbCbCCCCbbb", "b"]
Here Regex101 还显示以下内容作为警告:
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
我应该如何更改我的正则表达式模式?
也许试试这个:
A|b(C+)?
测试于 Notepad++
编辑:如果您想要此模式与组:
(A)|(b(C+)?)
如果您还想捕获 A
,只需用括号括起来:new Regex("(A)(bC*)*")
。见 regex demo.
然后,收集你在里面得到的所有值CaptureCollection:
var regex = new Regex("(A)(bC*)*");
var match = regex.Matches("AbCCbbCbCCCCbbb")
.Cast<Match>()
.SelectMany(x => x.Groups.Cast<Group>()
.SelectMany(v => v.Captures
.Cast<Capture>()
.Select(t => t.Value)
)
)
.ToList();
foreach (var s in match)
Console.WriteLine(s);