You're relying on a couple of things here, which may be OK and which may not be. If they are, disregard the code, which was written on the fly (but works with the sample input!). First, consider a line such as:

<img src="blern.gif" /><span style="display:block" >foo!</span>

That's going to pass your test. There's also that pesky problem that the tag need not all be on the same line:

<img src="gern.jpg" border="0" style="display:block" />

Pile on top of that the problem that it may already have a style element that doesn't contain the "display:block" and you're looking at trouble.

So you could sanitize your input to make sure none of these is the case, but then you might as well make the whole thing more CSS-friendly (image spacers indeed! =). Actually, better than adding the inline style would be creating a class in your stylesheet that specifies display:block. But I'm getting ahead of myself, and probably of where you want to be.

A more robust solution than any involving a hand-rolled regex would be to write a filter, presumably using HTML::Parser or a subclass thereof, that does the job, along the following lines:

#!/usr/bin/perl # note, warnings are off because this shouldn't generate # 'em except where there is no "style" attribute on # image tags. use strict; use HTML::Parser; my $p=HTML::Parser->new( default_h => [ sub { print $_[0] }, 'text' ], start_h => [\&start_tag, 't +agname, attr, text']); $p->parse_file(*DATA); sub start_tag { my($tagname, $attr, $origtext) = @_; if ( ! ( $tagname eq "img") ) { print $origtext; return; } if ($attr->{style} !~/display:block/) { # avoid messy issues with other styles that may have been defined $attr->{style} = "display:block;". $attr->{style}; } print "<$tagname"; foreach ( keys %$attr) { print qq{ $_="} .$attr->{$_}.qq{"}; } print ">"; } __DATA__ <html> <head> <title>Boo</title> </head> <body bgcolor="#434343"> <h3>Hello</h3> <img src="witches.jpg"> <img src="macbeth.jpg" style="border: thick double #fcfcfc;"> <img src="foofaraw" style="width: 90"> <img src="cauldron.jpg" style="display:block"> <img src="fenny_snake.png"><span style="display: block">Hi there</sp +an> </body> </html>

I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche


In reply to Re: How do I write a regex for 'does not contain' a string. by arturo
in thread How do I write a regex for 'does not contain' a string. by IraTarball

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.