in reply to Re: Re: Regex Strikes again!
in thread Regex Strikes again!

Try this:
use strict; undef $/;#In order to read the whole file at once open(F,"test.txt") or die $!; my $slurpedfile = <F>; close F; my @matches = $slurpedfile =~ m{ ( /\* .*? \*/) | ( \/\/[^\n]*) | " (?: [^"\\]* | \\. )* " | ' (?: [^'\\]* | \\. )* ' | . [^/"']* }xgs; @matches = grep {defined $_} @matches; #get rid of undefs my $linenum = 1; foreach my $match (@matches) { $slurpedfile =~ /\Q$match/; my $before = $`; $slurpedfile = $'; $linenum += $before =~ tr/\n/\n/; foreach (split "\n", $match) { print "Line $linenum\t$_\n"; $linenum++; } $linenum--; # the foreach above adds one too many } __OUTPUT__ Line 2 //Hello Line 3 // single line c++ comment Line 5 /* single line c comment */ Line 6 /* Line 7 multi line Line 8 c style comment */ Line 10 // another single line c++ comment

--

flounder

Replies are listed 'Best First'.
Re: Re^3: Regex Strikes again!
by nofernandes (Beadle) on Jul 16, 2003 at 16:11 UTC

    In fact your example worked perfectly in the tests that i´ve made!!

    Thank you very much for your help and patience!!You and the other monks gave to me new knowledges!! Thank you all

    Nuno