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

Hi I am trying to put wrappers around code blocks. The codeblock goes like this
<html> blah <?php xxx ?> blah <?=xxx?> blah <?php yyy blah ?> <body> </html>
The goal is find text between "?>" and "<?" and replace with "?> printf('" and "); <?" respectively. In this case, for the above example the output should be:
<html> blah '); <?php xxx ?> printf(' blah '); <?=xxx?>printf(' blah ); <?php yyy blah ?> printf(' <body> <html>
Any help with this multi-line regex match is much appreciated.
Thanks

20071201 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: multi line pattern match
by wind (Priest) on Nov 28, 2007 at 23:23 UTC
    Simple enough. You don't even truly need zero width look behind and look aheads, but I think that cleans up the logic some:
    use strict; my $data = do {local $/; <DATA>}; $data =~ s{(?<=\?\>)(.*?)(?=\<\?)}{printf('$1');}sg; print $data; __DATA__ <html> blah <?php xxx ?> blah <?=xxx?> blah <?php yyy blah ?> <body> </html>
    - Miller
      Thanks a lot everybody!
      I got it to work using the above.
Re: multi line pattern match
by jrsimmon (Hermit) on Nov 28, 2007 at 21:21 UTC
    Is there a reason you can't combine all of the lines into a single scalar to simplify your regex?
Re: multi line pattern match
by RaduH (Scribe) on Nov 28, 2007 at 21:37 UTC
    Use multi-line matching like this:
    m/^regex$/m
    The last m after regex turns on your "multi-line mode". Hope this helps.