For your first requirement, a regex is probably safe and effective, since (unless I'm having a Sr. moment) the html 4.x standard does not allow an image tag with a literal ">" inside the tag.

One way to approach the job, therefore, is to extend your regex with less-greedy (aka "minimally greedy") matching and a lookahead. Here's a sketch, minus file-handling, CGI, etc:

#!/usr/bin/perl use strict; use warnings; #825146 my @line = <DATA>; for my $line(@line) { chomp $line; if ( $line =~ m/(<img .*?[^>]+)/ ){ print "<g:image_link> " . $1 . "> </g:image_link>\n"; } else { print "\t nope: $line \n"; # you may want to send this to a di +fferent file } } __DATA__ <p><img src="http://www.mysite/graphics/blue.jpg" alt="Hey" width="100 +" height="100" ><br>yada yada</p> <p><img src="../grapics/blue1.gif" alt="Yo" width="200" height="75"></ +p> <p>foobar with no img</p> <blockquote><img width="75" height="75" src="blue2.png"></blockquote>

Output:

<g:image_link> <img src="http://www.mysite/graphics/blue.jpg" alt="Hey +" width="100" height="100" > </g:image_link> <g:image_link> <img src="../grapics/blue1.gif" alt="Yo" width="200" he +ight="75"> </g:image_link> nope: <p>foobar with no img</p> <g:image_link> <img width="75" height="75" src="blue2.png"> </g:image_ +link>

BUT take the advice from pemungkah above: Use a parser! Trying to deal with all the possible unwanted tags in a form with regexen is going to get you deeper and deeper into complexities.

And if you're planning to read user input from a form, for heaven's sake, read about untainting. You really don't want to let the fumble-fingered or malicious run around loose in your playground.


In reply to Re: getting and printing form values etc from html stripping out all else by ww
in thread getting and printing form values etc from html stripping out all else by kalkisong

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.