首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

如何用正则表达式过滤注释

2012-06-23 
怎么用正则表达式过滤注释?RT。例如:有一段代码:EMU0: in bit-- must be type in for compliance enable--

怎么用正则表达式过滤注释?

RT。

例如:有一段代码:
  EMU0 : in bit; -- must be type in for compliance enable
  -- non-JTAG type is inout
  EMU1 : in bit; -- must be type in for compliance enable
  -- non-JTAG type is inout
  RSV0 : in bit;
怎样用正则把注释过滤掉,其他的输出。注释就是"--"符号后边的!
 

[解决办法]

C# code
            string pattern = @"(?is)(?:--).*?(?:\n)";            string replacement = "\n";            string input = @"EMU0 : in bit; -- must be type in for compliance enable  -- non-JTAG type is inout  EMU1 : in bit; -- must be type in for compliance enable  -- non-JTAG type is inout  RSV0 : in bit;";            input = Regex.Replace(input, pattern, replacement);            Console.WriteLine(input);/*EMU0 : in bit;   EMU1 : in bit;  RSV0 : in bit;*/
[解决办法]
不考虑包在""中的--的话,这样就可以了
C# code
string test = @"EMU0 : in bit; -- must be type in for compliance enable  -- non-JTAG type is inout  EMU1 : in bit; -- must be type in for compliance enable  -- non-JTAG type is inout  RSV0 : in bit;";Regex reg = new Regex(@"--.*");string result = reg.Replace(test, "");richTextBox2.Text = result;
[解决办法]
单行注释是以换行为结束标志的,上述都是匹配行内的,不会换行匹配

热点排行