Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How to remove everything including newline characters, lying in between two patterns in perl? . .

Replies are listed 'Best First'.
Re: need a regex
by toolic (Bishop) on Jul 05, 2011 at 13:42 UTC
Re: need a regex
by AnomalousMonk (Archbishop) on Jul 05, 2011 at 18:57 UTC

    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.

Re: need a regex
by jethro (Monsignor) on Jul 05, 2011 at 15:23 UTC

    If I understand your question correctly:

    $string=~s/(pattern1).*(pattern2)/$1$2/s;
Re: need a regex
by i5513 (Pilgrim) on Jul 05, 2011 at 14:46 UTC
    Could you post an example ? I don't understand your question completly ..
Re: need a regex
by muba (Priest) on Jul 06, 2011 at 00:00 UTC
    I need a regex, a regex is what I need
    Well I need a regex, a regex is is what I need
    And I said I need regex, regex; a regex is what I need
    And if I share with you my story would you share your regex with me

    -- Aloe Blacc - I need a dollar

    Like said above, it sounds like you want the /s modifier.

    use strict; use warnings; my $foo = "blabla foo \n yadayada \n bar bla bla"; print "\n<--\n$foo\n-->\n"; $foo =~ s/foo.+bar//s; print "\n<--\n$foo\n-->\n"; __END__ Output: <-- blabla foo yadayada bar bla bla --> <-- blabla bla bla -->