在某个文件中有以下几种注释//comment
      //comment(前面有些空白)
code  //comment(前面有些代码)我需要一个正则表达式能够匹配前面两种注释,但是不可以匹配到第三种。
我是这样写的:(\s*//.*\n)
结果code后面的注释也被匹配上了,请问该怎么改,使得code后面的注释不被匹配上?

解决方案 »

  1.   

    测试
    string input = @"//comment
          //comment(前面有些空白)
    code  //comment(前面有些代码)";            var list = Regex.Matches(input, @"(?<=^|\n)\s*?//[^\n]*?(?=\n|$)").OfType<Match>().Select(a=>a.Value);            /*
                 *  [0] "//comment\r" string
    [1] "      //comment(前面有些空白)\r" string             */