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

In reply to Re: Re: Regex Strikes again! by nofernandes
in thread Regex Strikes again! by nofernandes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.