in reply to Replacing everything in between using s///;

Well, if you add /s to what you have, everything between and including $first and $last will be replaced. You say this didn't work for you?

Here's a useful mnemonic to remember the difference between /s and /m:

/s affects the meaning of a single regexp character (namely "."), while /m affects the meaning of multiple regexp characters (actually, just two: "^" and "$").
It's just a mnemonic, not the full story, but it comes in handy. With /m in effect, "^" matches not only the beginning of the string, but also the beginning of any line of text contained in the string (i.e. right after any embedded newlines). Likewise, with /m, $ will match the end of every line contained in the string, i.e. just before every newline contained in the string (though if the last character of the string is not a newline, then "$" matches the very end of the string). On the other hand, /s only makes "." match newlines too.

BTW, the parentheses in your regexp are superfluous, since you are not doing anything with the captured string, and in this case they are not needed for grouping either.

But despite my giving you these pointers on your regexp, I agree with merlyn that regexps are not the tool for this kind of problem.

the lowliest monk

  • Comment on Re: Replacing everything in between using s///;

Replies are listed 'Best First'.
Re^2: Replacing everything in between using s///;
by JayBee (Scribe) on May 09, 2005 at 10:32 UTC
    I just realilzed I had a typo, which is why it didn't work, Duh!

    Just in case you're curious, from s/$first(.*?)$last/$reserve/s;
    I took out "(.*?)$last" and tried it, worked in a funny way.
    so I tried it without the "$first(.*?)" instead, and it failed completely.

    So looking at my actual code, I found a space between the used example: "/ span>" for $last.

    So the little things will get ya if you are not getting enough sleep :)

    Thank you very much for building my confidence with the way I was sure to be right with previous help from all you perl monks.