Hey, there is something that has been bothering me for a long time. Bothering only, not making me unable to use Perl, because usually I found ways to "do it differently".

The problem is simple: I have a long string, with the word that I want to match several times in it.

Example: I open a file, which is itself a Perl Script, I read it in an array and join("",@) it in a single string. Let's say that I want to get rid of the statements that do the print "Content-type:text/html\n\n"; stuff. There can be several statements like that, and they can have different forms (print-one-space-string, print-as-many-spaces-as-you-want-string, print-parenthesis-string, etc.).

My approach is: I see that the expression I want to get rid of starts with print, and then the relevant string. So I want to match something that starts with print and end with the string, but how can I be sure that I am matching the print which is the closest to the string? (I don't want to get rid of a big chunck of my file that starts with any print and end with the string).

Thanks for your help (it must be an easy question for a real Perl Monk!)

In reply to 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.