Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# and Regex - Unrecognized grouping construct

Tags:

c#

regex

Currently working on my thesis for a client/server app. I've run into a snag were the server receives information like this:

ProToCooL,unknown|DESKTOP-29COFES,10.20.9.53|Hewlett-Packard,179C,PCWJA001X3UHII,KBC Version 42.32|i5-3320M,2.60GHz,2,4,256,3072,U3E1,GenuineIntel|{2,(IDT High Definition Audio CODEC,IDT),(Intel(R) Display Audio,Intel(R) Corporation)}|{2,(Samsung,003355A3,M471B5773DH0-CH9  ,24,2147483648,1333),(Hynix/Hyundai,467CA639,HMT351S6EFR8A-PB  ,24,4294967296,1333)}|{1,(IDE,Hitachi HTS725050A7E630,      FT51009Y7J61BK,3,733004892,476937.531738281)}|{7,(Send To OneNote 2013,False,Local,Send to Microsoft OneNote 15 Driver),(Microsoft XPS Document Writer,False,Local,Microsoft XPS Document Writer v4),(Microsoft Print to PDF,False,Local,Microsoft Print To PDF),(HP Universal Printing PCL 6,False,Local,HP Universal Printing PCL 6),(HP LaserJet P3010 Series (10.20.9.36),False,Local,HP Universal Printing PCL 6),(Fax,False,Local,Microsoft Shared Fax Driver),(Adobe PDF,True,Local,Adobe PDF Converter)}

And I have the current pattern set up:

\(((?:[^()]|(?R))+)\)

which does a decent job of extracting everything that I need, but the problem is that when i use it in C# in this way:

if(soundCount > 0)
        {
            string[] smth;
            string pattern = @"\(((?:[^()]|(?R))+)\)";
            Regex match = new Regex(pattern, RegexOptions.None);

            MatchCollection matches = match.Matches(sound);
            smth = new string[matches.Count];

            int i=0;
            foreach (Match ma in matches)
            {
                smth[i] = Regex.Replace(ma.Value.Trim('(', ')'), @"\s+", "");
                Console.WriteLine(smth[i]);
                i++;
            }


            IList<sound_chips> newSoundchip = new List<sound_chips>();
            foreach(string s in smth)
            {
                newSoundchip.Add(new sound_chips() 
                { 
                    name = s.Split(',')[0].ToString(), 
                    manufacturer_id = Convert.ToInt32(setManufacturer(s.Split(',')[1].ToString())),
                    motherboards = newMotherboard
                });
            }
            insert.sound_chips.AddRange(newSoundchip);
        }

I get an: Unrecognized grouping construct. exception and I'm having difficulties finding what's wrong cuz I don't really understand regexp for C# that much or find a way around it for that matter.

like image 568
ProToCooL Avatar asked Feb 09 '23 04:02

ProToCooL


1 Answers

.NET regex does not support recursion. The regex you are using (\(((?:[^()]|(?R))+)\)) is for PCRE, and you can use it with PCRE.NET library in a C# app.

As an alternative, you can use a .NET regex to match balanced parentheses:

\(((?>[^()]+|\((?<n>)|\)(?<-n>))+(?(n)(?!)))\)

See regex demo.

It returns all those 12 matches as the PCRE regex.

like image 163
Wiktor Stribiżew Avatar answered Feb 22 '23 08:02

Wiktor Stribiżew