0

I have N patterns that I want to match with one single regex, each pattern has an individual regex. What I'm doing right now is joining each regex with the '|' operator and giving each individual regex a group name.

In the example below I'm looking to get the matched group (Stream, Triple or Double) without having to iterate through all the groups in the match. Is this possible ?

/* language=regex */
@"(?<Stream>((C[b-i][0-3][5-9]){7,}(C[b-i][0-3].)?)+)" + 
 "|(?<Triple>(Ca[0-4].)(Ca[0-4].)(C...))" +
 "|(?<Double>(Ca[0-4].)(C...))",
  • `Regex.Match()` returns a `Match` which can allow you to access named groups. See https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.match?view=net-5.0 and https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match – Martin Costello Jun 05 '21 at 07:40
  • `match.Groups[0]` where match is [Match object](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match.groups?view=net-5.0) – Mat J Jun 05 '21 at 07:42
  • match.Groups[0] returns the group of the whole regex not the first named group –  Jun 05 '21 at 07:43
  • It is as easy as `match.Groups["Stream"].Value`, etc. – Wiktor Stribiżew Jun 05 '21 at 08:28
  • well thats the thing I don't know which pattern I match, u are assuming I know that I will match a Stream but only one of these pattern will match –  Jun 05 '21 at 08:34

0 Answers0