Konrad Viltersten Konrad Viltersten - 1 month ago 4
C# Question

Regex to match for a string but only bringing a part of it by grouping

I have a string on the following form - for fiddle see here.


I would like to filter out just the coordinates in the parentheses. So, the pattern to match I've set up recognized a pair of parentheses (two backslashes for escaping the grouping). Then, it creates a group that on its inside has the minimal set of characters as long as they're separated by a semicolon and enclosed in parentheses. According to the debugger I have four matches, which suggest that it's correct. However, when I access Grouping, I see two elements - one containing a pair of brackets and one without.

Regex regex = new Regex("\\((.*?;.*?)\\)");
string full = regex.Matches(figure.ToString())[0].Value;
string with = regex.Matches(figure.ToString())[0].Groups[0].Value;
string sans = regex.Matches(figure.ToString())[0].Groups[1].Value;

I'm not entirely sure where the first group (Groups[0]) gets its information from. I suspect that I haven't phrased the regular expression well enough and that it actually react to the escaped parentheses as if it was a grouping as well. Am I right in my suspicion? How should I reformulate the expression?


From https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.match.groups(v=vs.110).aspx:

If the regular expression engine can find a match, 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 Groups[0] has the entire value you matched (e.g. (1;2)), while Groups[1] is the first matched subgroup (e.g. 1;2).