Regex in C # acts weird

I ran into a problem while working with regex in C #. Namely, the debugger shows the correct results (IMO), but when I try to print the results in my application, they are different (and incorrect). Code below:

Match match2 = Regex.Match("048 A Dream Within A Dream (satur) (123|433) K48", "(.*)(\\((.)*?\\))\\s\\((.)*?\\)\\s.*"); string nick = match2.Groups[1].Value; string name = match2.Groups[0].Value; Console.WriteLine("nick - '{0}', name - '{1}'", nick, name); 

The expected results are displayed in the debugger, as shown in the following screenshot: enter image description here

The console shows different (incorrect) results:

nick - '048 Dream in a Dream', name - '048 Dream inside A Dream (Satur) (123 | 433) K48'

How to fix it? I want the results displayed exactly the same as in the debugger.

+4
source share
2 answers

You are missing the fact that Groups[0] always intended to represent an entire match. The first capture group is in Groups[1] . Do you want to:

 string nick = match2.Groups[2].Value; string name = match2.Groups[1].Value; 

The reason it shows what you expected in the debugger is because you are looking at the implementation detail of a field inside GroupCollection ; when he requested a group by number, it returns a match if the requested number is 0 or otherwise shifts the number by 1.

From the documentation for GroupCollection :

If the match is successful, the first item in the collection contains a Group object that matches the entire match. Each subsequent element represents a captured group if the regular expression includes capture groups.

+9
source

You look in the _groups field, but this is not quite what is returned as the Groups property:

enter image description here

Change your code to use Groups[1] and Groups[2] :

 string nick = match2.Groups[2].Value; string name = match2.Groups[1].Value; 
+3
source

Source: https://habr.com/ru/post/1500209/


All Articles