tags:

views:

130

answers:

3

Hello.

I have a text file including the lines below:

/* MY TXT File LINE */
/* MY TXT File LINE MORE */

constant private FileName = <A "C:\\TMP\\ALARM.TXT">

constant ConfigAlarms = <U1 0>    /*  Comment Here*/

I don't know how to divide the comment lines (/* something */):

LINE1:

/* MY TXT File */

LINE2: (acturally i don't think the line is a really comment line.)

constant ConfigAlarms = <U1 0>    /*  Comment Here*/

my code below.

if ($val =~ /\/\*/){
    print "<!-- $line -->";
    print "\n";
}

LINE1 and LINE2 will be treated as comment.

I googled and found the information below.

^f     # f at the beginning of a line
^ftp   # ftp at the beginning of a line
e$     # e at the end of a line
tle$   # tle at the end of a line

but i don't know how to combine /* with the ^ and $ character to improve my code to parse the Line starting with /* and end with */

Thank you.

+4  A: 
m{^/\*.*\*/$}

will match lines that have /* as their first two characters and */ as their last two.

Anon.
@Nano, it's important to note that Anon's suggestion uses {} instead of // in the regular expression. It makes the regular expression easier to read.
pavium
Also note that, like most regex solutions, this will fail if you happen to have a string in your source code that contains `"/* comments */"`. If this is a problem, a parser is in order.
Chris Lutz
/*MY TXT File */ -- There is no blank space between * and M, it works well now./* MY TXT File */ -- There is one or more than one blank spaces between * and M, it doesn't work.How to handle it? thanks.
Nano HE
Unless something else in your script is mangling the input, this should work fine regardless.
Anon.
This non-greedy multi line version will allow for multi line comments `m{ ^ /\* .*? \*/ $ }xms` Save you some *//*'s. :)
zen
@Anon,Acturally, my TXT file is very large size. i must handle both the comment style above. And there file was created automatically, all my team members take use it. That's why i must think about the two conditions.
Nano HE
I don't think you actually understood what I wrote. So I'll say it straight this time: *the regex I posted will handle both styles*. Either there are problems elsewhere in your script, or the text file has characters before the opening / or after the closing one.
Anon.
@Anon, Yes, i found my script bug. thanks a lot.
Nano HE
Awesome. Good luck with whatever it is you're doing.
Anon.
+2  A: 

See this FAQ. Instead of ignoring text matching the given pattern, capture it.

Alternatively, you can give String::Comments::Extract a shot. See String::Comments::Extract::C->collect( <source> ).

Sinan Ünür
+2  A: 

There is an incredible CPAN module that can help with this, Regexp::Common::comment. Using it is incredibly easy:

use Regexp::Common qw /comment/;

while (<>) {
    /$RE{comment}{C}/       and  print "Contains a C comment\n";
    /$RE{comment}{C++}/     and  print "Contains a C++ comment\n";
    /$RE{comment}{PHP}/     and  print "Contains a PHP comment\n";
    /$RE{comment}{Java}/    and  print "Contains a Java comment\n";
    /$RE{comment}{Perl}/    and  print "Contains a Perl comment\n";
    /$RE{comment}{awk}/     and  print "Contains an awk comment\n";
    /$RE{comment}{HTML}/    and  print "Contains an HTML comment\n";
}

use Regexp::Common qw /comment RE_comment_HTML/;

while (<>) {
    $_ =~ RE_comment_HTML() and  print "Contains an HTML comment\n";
}

You should be able to easily extend this to cover multi-line comments.

Joe Casadonte