in reply to Regex Strikes again!

This code might point you in the right direction.
use strict; my $slurpedfile = q{Blah blah blah blah blah Blah // single line c++ comment more blahs "quoted string with /* " blah blah blah blah /* single line c comment */ blah blah blah /* multi line c style comment */ some more blahs // another single line c++ comment blah blah blah }; 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 = $'; my $matched = $&; $linenum += $before =~ tr/\n/\n/; print "$match\n is on line $linenum\n\n"; $linenum += $match =~ tr/\n/\n/; } __OUTPUT__ // single line c++ comment is on line 2 /* single line c comment */ is on line 4 /* multi line c style comment */ is on line 5 // another single line c++ comment is on line 9
It will screw up if you have the same comment in a quoted string near the comment itself, like this:
comment = ' /* comment */ '; /* comment */
but maybe you can fiddle with $slurpedfile =~ /\Q$match/; to take care of that case.

--

flounder

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

    Thank you very much for your help!

    Your code works fine with the variable.. but when i try to read a file, the number of the lines are wrong!

    For example if i put the content of your variable $sluperdfile and add only one line such as //Hello:

    #Content of the file test.txt Blah blah blah //Hello blah blah Blah // single line c++ comment more blahs "quoted string with /* " blah blah blah blah /* single line c comment */ blah blah blah /* multi line c style comment */ some more blahs // another single line c++ comment blah blah blah

    And now the source code:

    use strict; undef $/;#In order to read the whole file at once open(F,"test.txt"); my @matches = <F> =~ m{ ( /\* .*? \*/) | ( \/\/[^\n]*) | " (?: [^"\\]* | \\. )* " | ' (?: [^'\\]* | \\. )* ' | . [^/"']* }xgs; @matches = grep {defined $_} @matches; #get rid of undefs my $linenum = 1; foreach my $match (@matches) { $slurpedfile =~ /\Q$match/; my $before = $`; $slurpedfile = $'; my $matched = $&; $linenum += $before =~ tr/\n/\n/; print "Line $linenum\t$match\n"; $linenum += $match =~ tr/\n/\n/; }

    How can i grab the line numbers correctly! What variable should i put instead of $slurpedfile? According that i want to read directly from a file?

    And another issue.. Is it possible to the output be something like this:

    Line 12 //Hello Line 23 // single line c++ comment Line 34 /* single line c comment */ Line 45 /* Line 46 multi line Line 47 c style comment */ Line 58 // another single line c++ comment

    Instead of:

    Line 12 //Hello Line 23 // single line c++ comment Line 34 /* single line c comment */ Line 45 /* multi line c style comment */ Line 58 // another single line c++ comment

    Thank you.. for your help...

    Nuno
      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

        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