Regex in .NET: joining duplicate named captured groups
Given the expression ^(?<res>a).*(?<res>c)
and test string abc
, expected named group res
to concat both found values and get ac
, but got latest change - c
.
Any way for C#'s regex class to support concat for named groups within regex?
Related question is Regex issue with named captured pairs, and by the chance it says that Perl/PCRE does not supports duplicate named pairs, but here I got .NET, and looking for it's specific magic to make regex return a开发者_运维百科 single match that contains both found values from different parts of string (that is, abbbbbcdef should return ac).
Calling regex more than once or joining resulting groups in code is not a well-tolerated solution now - looking to do the whole job inside regex.
The purpose of non-unique group names is merely to provide more flexibility in capturing parts of the string. Taking the captured parts and reassembling them differently is something you do after the regex has matched--typically with the Replace
method:
string s0 = @"abbbbbcdef";
string s1 = Regex.Replace(s0, @"^.*(a).*(c).*$", "$1$2");
Console.WriteLine(s1);
output:
ac
This questions reminds me of others I've seen where people wanted the regex to "skip" the parts of the string they weren't interested in--that is, to consume some parts but not others. There's no way to do that in any of the regex flavors I'm familiar with.
var match = re.Match(s);
var captures = match.Groups["res"].Captures.Cast<Capture>().Select(c => c.Value);
var result = string.Concat(captures);
The Cast()
is necessary, because the collection that Captures
returns doesn't implement IEnumerable<T>
.
Current solution is to assign group names in ascending order for each condition: ((?<group0>))|((?<group1>)(?<group0>))
After matching, all found groups are put to list and sorted by their name, building a final query string from either group0,group1
or just group0
(depending on what condition was matched).
Another trick is the need to start matching from beginning:
^(?wastebytes(condition)(chance1)|(chance2))
精彩评论