This is a job for a full blown parser. You can look into Parse::RecDescent on CPAN.

This is because it sounds like you'll want to match even:

print "Bob was $frank \"yes!\" around"."No Way!", $foo, "\n"; #or print "joe" if $bar;
Which a normal regexp will be unable to follow easily.

That said, if this is a one-time job with certain rules you can rely on, there are ways to do what you want. For example, if every statement ends with a semi-colon, and the word "print" is never used in strings in the Perl script, you could try matching

/print[^;]*;/
But be aware that even something as simple as:
if($foo){ print "Bar" } # or $foo= "Ask frank to print the report";
Would mess that up. If you have a situation with many situations, use a real Parser.

Note also that you might mess up the logic of a program. See:

print "Foo\n" while &func($bar);
If your program relies on &func($bar) being called several times, you would have eliminated the entire statement if using the simple regexp; Thanks to lhoward for introducing me to Parse::RecDescent

In reply to Re: A Reg Exp Question by swiftone
in thread A Reg Exp Question by Anonymous Monk

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.