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,889
  • 18
  • 64
  • 95
  • Try `(?m)(?<=;\s*|^\s*)(?<!/)//(?!/).*$`, but there might be still issues with the regex. Also, please check the approach [here](http://stackoverflow.com/questions/3524317/regex-to-strip-line-comments-from-c-sharp). – Wiktor Stribiżew Aug 21 '15 at 14:57

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
  • 21,247
  • 6
  • 47
  • 54
2

use this patten

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

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

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

Sky Fang
  • 1,101
  • 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,749
  • 2
  • 22
  • 37
1

Try this regex:

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

First group should give you the commented line.

Demo

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
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,138
  • 7
  • 31
  • 44
-3

The expression should be like this:

//.*
Ced
  • 1,301
  • 11
  • 30