Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Re: Regex Strikes again!

by nofernandes (Beadle)
on Jul 16, 2003 at 14:34 UTC ( [id://274838]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^3: Regex Strikes again!
by flounder99 (Friar) on Jul 16, 2003 at 15:31 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://274838]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (None)
    As of 2024-04-25 01:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found