exclude comments using Regular Expressions

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2006
Posts: 4
Reputation: shuchimuley is an unknown quantity at this point 
Solved Threads: 0
shuchimuley shuchimuley is offline Offline
Newbie Poster

exclude comments using Regular Expressions

 
0
  #1
May 2nd, 2007
Hi ,

I wanted to know how to write a c# code that reads from a file and counts the number of lines . This count should exclude the comments present

eg :

//this is test
using System;

should not count "//this is test" at all .

Please can someone help me on this one

Thanks
shuchi
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: exclude comments using Regular Expressions

 
0
  #2
May 2nd, 2007
you could use the line.BeginsWith or line.StartsWith (forgot which one it is), and pass in "//". If it returns true, then don't count the line and go to the next line.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: exclude comments using Regular Expressions

 
0
  #3
May 2nd, 2007
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 4
Reputation: shuchimuley is an unknown quantity at this point 
Solved Threads: 0
shuchimuley shuchimuley is offline Offline
Newbie Poster

Re: exclude comments using Regular Expressions

 
0
  #4
May 3rd, 2007
i gotta use regex.match(regularexpression) . I am using a streamreader to read the whole file and then the contents of the file are match with the pattern of the regular expression . and this regular expression needs to count all the lines of code excluding watever is written in comments .

eg

//test1
//test2
/* test3 */
using System;
using System.Collections.Generic; /* test4 */
using System.Text;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
namespace Regular_Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Good");
}
}
}

this shud remove all the comments .. ie shud count all the line of code except comments (/*..*/) , (//) wherever they appear .
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: exclude comments using Regular Expressions

 
0
  #5
May 3rd, 2007
Well the idea would be to write a very trivial regex expression and then apply it on a code snippet. Then you would add to that regex expression gradually getting more complex.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: exclude comments using Regular Expressions

 
0
  #6
May 3rd, 2007
sounds like homework. Show us what you have tried and then we can help
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: exclude comments using Regular Expressions

 
0
  #7
May 4th, 2007
  1. (//.*\n|/\*(.|\n)*?\*/)

untested...
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 4
Reputation: shuchimuley is an unknown quantity at this point 
Solved Threads: 0
shuchimuley shuchimuley is offline Offline
Newbie Poster

Re: exclude comments using Regular Expressions

 
0
  #8
May 15th, 2007
well well well ,
i made a C# program that now reads a file and excluding the comments out.. that worked .. thanks to all .
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC