c#中的正则表达式,包括注释
我有一个字符串:c#中的正则表达式,包括注释,c#,regex,comments,C#,Regex,Comments,我有一个字符串: String str = { { /*start address*/ 0x00004200, /*Length*/ 0x00003CBF } , { /*start address*/ 0x00004200, /*Length*/ 0x00003CBF } }; 我需要使用正则表达式验证上面的字符串 我写的正则表达式是: ^{ *{ *([/*]{1} *(\S* *)* *[*/]{1} *)?(0x)?[0-9a-fA-F]{1,8}, *([/*]{1} *(\S* *
String str = { { /*start address*/ 0x00004200, /*Length*/ 0x00003CBF } , { /*start address*/ 0x00004200, /*Length*/ 0x00003CBF } };
我需要使用正则表达式验证上面的字符串
我写的正则表达式是:
^{ *{ *([/*]{1} *(\S* *)* *[*/]{1} *)?(0x)?[0-9a-fA-F]{1,8}, *([/*]{1} *(\S* *)* *[*/]{1} *)?(0x)?[0-9a-fA-F]{1,8} *} *( *,\s*{ *([/*]{1} *(\S* *)* *[*/]{1} *)?(0x)?[0-9a-fA-F]{1,8}, *([/*]{1} *(\S* *)* *[*/]{1} *)?(0x)?[0-9a-fA-F]{1,8} *})*\s*} *;$
我知道这有点复杂。我不能把它弄对。应用程序在Regex.Match行崩溃
我认为问题在于字符串和regex w.r.t之间有/*和*/这是不正确的。如下更改您的regex
^{ *{( *[/*]{2} *(?:(?!/\*|\*/).)*\*/ *(?:0x)?[0-9a-fA-F]{1,8} *)(?:, *[/*]{2} *(?:(?!/\*|\*/).)*\*/ *(?:0x)?[0-9a-fA-F]{1,8} *)} *, *{ *[/*]{2} *(?:(?!/\*|\*/).)*\*/ *(?:0x)?[0-9a-fA-F]{1,8} *, *[/*]{2} *(?:(?!/\*|\*/).)*\*/ *(?:0x)?[0-9a-fA-F]{1,8} *} *};$
试试这个
string txt="{ { /*start address*/ 0x00004200, /*Length*/ 0x00003CBF } , { /*start address*/ 0x00004200, /*Length*/ 0x00003CBF } }";
string re1="(\\{)"; // Any Single Character 1
string re2=".*?"; // Non-greedy match on filler
string re3="(\\/\\*[\\d\\D]*?\\*\\/)"; // C Comment 1
Regex r = new Regex(re1+re2+re3,RegexOptions.IgnoreCase|RegexOptions.Singleline);
Match m = r.Match(txt);
尝试在调试它,在那里可以输入regexp并查看它的可视化表示,然后输入数据以测试它。这比用C语言做要容易得多。谢谢你,马修。我试过这个工具。但是我无法理解它的工作原理。@MatthewWatson我可以使用这个工具,并计算出另一个正则表达式。谢谢分享工具参考。这帮了大忙!!谢谢阿维纳什。我试过你的密码。但有一个例外:“无法识别的分组构造”。