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 -->";
pr开发者_JS百科int "\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.
m{^/\*.*\*/$}
will match lines that have /*
as their first two characters and */
as their last two.
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> )
](http://search.cpan.org/perldoc/String::Comments::Extract#String::Comments::Extract::C-%3Ecollect(%3Csource%3E).
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.
精彩评论