944,008 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 8544
  • C# RSS
May 2nd, 2007
0

exclude comments using Regular Expressions

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shuchimuley is offline Offline
4 posts
since Dec 2006
May 2nd, 2007
0

Re: exclude comments using Regular Expressions

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.
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
May 2nd, 2007
0

Re: exclude comments using Regular Expressions

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 3rd, 2007
0

Re: exclude comments using Regular Expressions

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 .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shuchimuley is offline Offline
4 posts
since Dec 2006
May 3rd, 2007
0

Re: exclude comments using Regular Expressions

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 3rd, 2007
0

Re: exclude comments using Regular Expressions

sounds like homework. Show us what you have tried and then we can help
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
May 4th, 2007
0

Re: exclude comments using Regular Expressions

C# Syntax (Toggle Plain Text)
  1. (//.*\n|/\*(.|\n)*?\*/)

untested...
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 15th, 2007
0

Re: exclude comments using Regular Expressions

well well well ,
i made a C# program that now reads a file and excluding the comments out.. that worked .. thanks to all .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shuchimuley is offline Offline
4 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: System.NullReference Exception
Next Thread in C# Forum Timeline: Dropdownlist.... please help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC