C#和Regex-无法识别的分组结构 [英] C# and Regex - Unrecognized grouping construct

查看:35
本文介绍了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))+)\)

在提取我需要的所有东西方面做得不错,但是问题是当我以这种方式在C#中使用它时:

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);
        }

我得到一个:无法识别的分组结构.异常,我很难找到问题所在,因为我对C#的regexp不太了解,也无法为此找到解决方法.

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.

推荐答案

.NET regex不支持递归.您正在使用的正则表达式( \((((?:[^()] |(?R))+)\)\) )用于PCRE,您可以将其与C#中的 PCRE.NET库一起使用应用程序.

.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.

或者,您可以使用.NET正则表达式来匹配平衡的括号:

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

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

请参见它将所有这12个匹配项作为 PCRE正则表达式返回.

It returns all those 12 matches as the PCRE regex.

这篇关于C#和Regex-无法识别的分组结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆