nofernandes has asked for the wisdom of the Perl Monks concerning the following question:
I have this regex that tries to get the lines of a program with comments..
sub extract_comments { my $filename = shift; local (*F, $/); open F, "< $filename" or warn("can't read $filename: $!"), return; @hello1=grep defined, <F> =~ m{ ( \# .*? \n ) # extract a comment starting in # #and finishing in an enter | " (?: [^"\#]* | \#. )* " # skip over "..." | ' (?: [^'\#]* | \#. )* ' # skip over '...' | . [^\#"']* # skip over non-comments-or-quotes }xgs; $tam=@h...; print "Tam: $tam\n"; return @hello; } $file="ex1.pl"; @ola=extract_comments($file); foreach $line (@ola){ print"$line\n"; }
But the problem is that this doesn´t work very well!! Can somebody tell me why?
Another thing is how can i catch the line number when the regex has match??
Thank you all!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Bad Regex
by Theo (Priest) on Jun 23, 2003 at 18:45 UTC | |
|
Re: Bad Regex
by RollyGuy (Chaplain) on Jun 23, 2003 at 18:02 UTC | |
by Abigail-II (Bishop) on Jun 24, 2003 at 02:39 UTC | |
|
Re: Bad Regex
by Popcorn Dave (Abbot) on Jun 23, 2003 at 18:58 UTC | |
|
Re: Bad Regex
by Not_a_Number (Prior) on Jun 23, 2003 at 19:37 UTC | |
by Anonymous Monk on Jun 23, 2003 at 20:10 UTC | |
|
Re: Bad Regex
by graff (Chancellor) on Jun 24, 2003 at 02:32 UTC | |
|
Re: Bad Regex
by Anonymous Monk on Jun 23, 2003 at 19:04 UTC |