Home > OS >  .NET regex match returning too many elements
.NET regex match returning too many elements

Time:12-19

Per enter image description here

A bit more specific pattern could be:

(\S )[\p{Zs}\t] \(([0-9] )\)
  • (\S ) Capture group 1, match 1 non whitespace chars
  • [\p{Zs}\t] Match 1 or more spaces (\s can also match a newline)
  • \(([0-9] )\) Capture group 2, match 1 digits 0-9 between matchint the ( and )

.NET regex demo

CodePudding user response:

You did it correctly. According to .NET documentation:

the first element of the GroupCollection object (the element at index 0) returned by the Groups property contains a string that matches the entire regular expression pattern

So, regex pattern with 2 groups will return 3 results:

  1. string that matches the pattern
  2. 1st group
  3. 2nd group
  • Related