views:

119

answers:

2

Hi,

can somebody tell me how to avoid commented lines when using a regular expression in Visual Studio? I tried ^[^//]* but it deosn't work.

For example, I want to omit following line when I search:

//Hello
+1  A: 

This should work:

(:?//[^\n]*|/\*.*\*/)

update added some sample code

using System; using System.Text.RegularExpressions;

namespace ConsoleApplication {
    class Program {
     static void Main(string[] args) {
      Regex commentsFilter = new Regex(@"(:?//[^\n]*|/\*.*\*/)");
      string sample = ""
       + "a\n"
       + "//b\n"
       + "/*c*/\n"
       + "d";
      string filteredSample = commentsFilter.Replace(sample, "");
      string[] lines = filteredSample.Split('\n');
      foreach (string line in lines) {
       Console.WriteLine(line);
      }
      Console.ReadKey();
     }
    }
}
Kristof Neirynck
A: 

You should be able to use the "Prevent Match" syntax "~()" in Visual Studio:

^~(//).*

Perhaps you want to allow optional spaces or tabs at the beginning of lines not to match:

^:b~*(//).*

For information the ~() operator is a negative lookahead assertion, in conventional regex syntax (rather than VS) this would be written as:

^\s*(?!//).*
Callum