skip to Main Content

As per this document there is change in regex methodology from vs 2010 to vs 2012.

Using below Regex I am able to exclude all the comment in VS2010 for all cases

^~(:b*').*your_search_term.

But when we try below regex (After changes suggested in the doument) i am able to Exclude comments from Visual Studio Find’s search results but only for some cases.

^(?![ t]*[']).*your_search_term.    
^(?!(s*')).*your_search_term.
^(?!(?([^rn])s)*').*your_search_term.

above Regex fails for cases when comments are in the same line as code.

ex: Try  ' On Error GoTo test_EH

Is there any way to Find by excluding all the comments using Visual Studio 2017 Find?

Edited:
Cases we have checked regex For:

1. when comments are in the same line as code.
   ex: Try  ' On Error GoTo test_EH

2. When only Comment present in a line
   ex: 'On Error GoTo test_EH

3. When there are words within ' ' in code
   ex: Dim str1 As String = " 'test_EH' "

Edited:

modifying the regex mentioned in this Link , I could able to detect all the comments in the project which include test word.

(?mn)^(?<line>[^rn"']*(("[^"]*")[^rn"]*)*)(')[^rn]*test[^rn]*

but negating that regex to exclude comment will also exclude the below case:

Dim test a string 'this is test example

How to modify this regex to exclude comments properly?

2

Answers


  1. Chosen as BEST ANSWER

    I have combined and created the regex for vb.net language which works for all above mentioned cases in VS2017.

    Regex:

    (^(?!.*').*mexico)|(("(.*(mexico).*)+?")|('(.*(mexico).*)+?'))
    

  2. I am not familiar with that old syntax BUT based on your requirement a new regex can be built such as:

    (?:^s*|s+)'(?!.*').*test_EH
    

    Basically, this will match any:

    • line starting with '
    • line ending with a text proceeded by a single '

    In both cases, only 1 ' is allowed AND the text test_EH must be found as well

    DEMO:

    https://regex101.com/r/wj7M9t/1

    Please comment with more details/examples to fine-tune it if needed

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search