in reply to Regex to "wrap" a <span around an image.

I tend to stick to regexps for simple things like this and only move to the CPAN modules when processing of nested tags is required. Image tags in HTML are complete and cannot contain any other tags within. So, a regexp solution:

my $class = "large-image-right-landscape"; $content =~ s{ ( # start capture <img\s # opening of img tag [^>]* # everything to the tag end > # tag end ) # end capture }{ <span class="$class">$1</span> }sigx;

The /sigx means not to treat newlines specially, ignore case, perform the replacement globally on the string, and use the layout given above with comments interspersed.

Replies are listed 'Best First'.
Re^2: Regex to "wrap" a <span around an image.
by Skeeve (Parson) on Nov 27, 2008 at 22:54 UTC

    You know that your regex will fail?

    Example HTML code snippet:

    <img src="greater.gif" alt=">">

    Yes, it's not nice to put an unescaped > there, but it's legal and I see it more than often when dealing with jsf and woodstock tag library.


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      Ah never saw that before. Thanks for pointing that one out! Technically shouldn't that be encoded as <img src="..." alt="&gt;">? I think you'll find my solution shouldn't fail unless you use data from people determined to make your life miserable.

        Sure I know. I use that regexp more than once in a while. But Sun is determined to make my life miserable ;-) (just kidding)


        s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
        +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e