4

My project is developed by many people. Many of the developers have commented few of their codes

I have a lot of codes like

        //ServiceResult serviceResult = null;
        //JavaScriptSerializer serializer = null;
        //ErrorContract errorResponse = null;

They use // ,they don't use /**/ How can I find all such commented line in visual studio 2012 using regular expression

In that find it should not find any xml comments with ///

Kuttan Sujith
  • 7,811
  • 18
  • 63
  • 92

6 Answers6

3

Simply try as

(?<!/)//.*(?!/)
  • (?<!/) Negative Lookbehind - To check the // doesn't contains / as a preceding character
  • //.* Matches any character including // except newline
  • (?!/) Negative Lookahead - To check the // doesn't contains / as a next character
Narendrasingh Sisodia
  • 20,667
  • 5
  • 43
  • 53
2

use this patten

(?<!/)//(?!/)

(?<!/) means it can not be / before //

(?!/) means it can not be / after //

Sky Fang
  • 1,081
  • 6
  • 6
  • your expression doesn't work, '/' need to be escaped – maraaaaaaaa Aug 21 '15 at 14:48
  • @Andrew: it should not be escaped. And as for explanation: the expression matches 2 consecutive `/`s that are not enclosed into `/`s. However, this regex is not the right answer, I am afraid. – Wiktor Stribiżew Aug 21 '15 at 14:48
  • First of all.. yes it does... https://regex101.com/r/mT9tK8/1, second of all **this doesn't even capture the commented text, it only captures the dashes** – maraaaaaaaa Aug 21 '15 at 14:51
  • @Andrew: Please check the tags: it is .NET, not PCRE. You cannot test .NET regex at regex101.com. And I do not say it is correct. Same as yours. – Wiktor Stribiżew Aug 21 '15 at 14:52
  • Okay if it is `.NET` then i agree, but are we assuming `.NET` just because of the `c#` tag? And the second comment was @KuttanSujith – maraaaaaaaa Aug 21 '15 at 14:54
1

Try this expression (?<!\/)\/\/[^\/].*

and for .NET as someone mentioned: (?<!/)//[^/].*

maraaaaaaaa
  • 7,355
  • 2
  • 19
  • 33
1

Try this regex:

^\s*(?<!/)(//(?!/).+)$

First group should give you the commented line.

Demo

Community
  • 1
  • 1
NeverHopeless
  • 10,869
  • 4
  • 34
  • 55
1

This should cover most spacing cases and work in all VS versions. I believe look-behinds are only supported in VS2013.

^(?:\s|\t)*?//(?!/\s*<).+$
Pierluc SS
  • 3,038
  • 7
  • 30
  • 44
-3

The expression should be like this:

//.*
Ced
  • 1,281
  • 9
  • 30