Another way, using a positive look-ahead assertion and the  \K special escape of 5.10+ (see  (?<=pattern) \K in Look Around Assertions) which is effectively a variable-width positive look-behind (an actual look-behind in Perl regex may only be fixed-width):

>perl -wMstrict -le "my $s = 'there is no FOO stuff i do not want BAR unwanted BAR stuff here'; ;; my $t = $s; $t =~ s{ FOO \K .*? (?= BAR) }{}xms; print qq{'$t'}; ;; $t = $s; $t =~ s{ FOO \K .* (?= BAR) }{}xms; print qq{'$t'}; " 'there is no FOOBAR unwanted BAR stuff here' 'there is no FOOBAR stuff here'

The given examples illustrate the effect of the  ? quantifier modifier to achieve 'lazy' matching. If the substitution is to be done repeatedly, the  /g regex modifier should be used.

As suggested above, a positive look-behind could have been used in the examples because the pattern in question is fixed-width, but look-behind assertions, positive or negative, cannot be used for general patterns, which seemed to be the thrust of the OP.


In reply to Re: need a regex by AnomalousMonk
in thread need a regex 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.