8

In Visual Studio 2010, how do you search for text that is not within a single line comment? E. G. how to find "bas" in:

foo bar bas

but not in

foo bar // bas

Note that it should find the line:

foo / bar / bas

(edit) And it should not find the line:

foo // bar bas
Tony
  • 856
  • 1
  • 10
  • 17
  • Okay, so I asked this question just so I could refer back to my own answer. Unfortunately, I have to wait 8 hours to answer my own question. Visual Studio doesn't seem to have the typical look-ahead, look-behind constructs. It does have a similar zero-width negative assertion. The syntax is ~(x) which means the pattern does not match x at this point in the pattern. Using this contruct, I came up with this: ^(.~(//))*bas Which works really well, but won't match a line where // are the first two characters on the line. A version to fix that is: ^~(//)(.~(//))*bas – Tony Jan 26 '12 at 17:41
  • You asked a question just to answer it yourself? – iandotkelly Jan 26 '12 at 17:53
  • So, did you post that comment because I already answered your question? :) – Tony Jan 27 '12 at 21:30

2 Answers2

6

In the Visual Studio Find dialog, try using this regular expression (make sure to select Use: Regular expressions in the Find options):

~(//[.:b]*)<bas>

This should find all occurrences of the word bas which are not preceded by //.

Note that the Visual Studio regex syntax is a bit different than the conventional syntax. You can find the reference HERE.

cmbuckley
  • 33,879
  • 7
  • 69
  • 86
RoccoC5
  • 4,125
  • 14
  • 20
  • This doesn't work in the example: foo // bar / bas where bas is in a comment, and so should not be found. Thanks for the answer, though, and for the link. Also, the < and > word boundaries probably aren't necessary, although it depends on what you want. – Tony Jan 26 '12 at 17:44
  • 5
    For VS2012 users: "Visual Studio 2012 uses .NET Framework regular expressions to find and replace text. In Visual Studio 2010 and earlier versions, Visual Studio used custom regular expression syntax in the Find and Replace windows". Source: [Using Regular Expressions in Visual Studio](http://msdn.microsoft.com/en-us/library/2k3te2cs(v=vs.110).aspx) – tranquil tarn May 15 '13 at 13:02
6

Okay, so I asked this question just so I could refer back to my own answer.

Visual Studio doesn't seem to have the typical look-ahead, look-behind constructs. It does have a similar zero-width negative assertion. The syntax is ~(x) which means the pattern does not match x at this point in the pattern. Using this construct, I came up with this: ^(.~(//))*bas Which works really well, but won't exclude a line where // are the first two characters on the line. A version to fix that is: ^~(//)(.~(//))*bas

hawbsl
  • 13,626
  • 23
  • 67
  • 109
Tony
  • 856
  • 1
  • 10
  • 17