On May 14, Noah Sussman said:

>I am trying to write a simple script to insert ALT attributes into IMG tags
>that don't have them, leaving other IMG tags untouched.
>
>The problem is that I can't figure out how to tell Perl to search for
>strings beginning with "<IMG", ending with ">" AND not containing "ALT="
>(and it has to do this over multiple lines as well!).

First, you need to construct a proper IMG-tag-matching regex.

  my $value = qr{
    " [^"]* " |  # a double-quoted value
    ' [^']* ' |  # a single-quoted value
    [^\s>]*      # an unquoted value
  }x;

  my $attr = qr{
    \s*       # 0 or more whitespace
    \w+       # the attribute
    (?:
      \s*     # 0 or more whitespace
      =       # an '='
      \s*     # 0 or more whitespace
      $value  # its value
    )?        # but that part's optional
  }x;

  my $tag = qr{
    <img      # the start of the tag
    (?:
      \s+     # 1 or more whitespace
      $attr*  # 0 or more attributes
    )?        # but that part's optional
    \s*       # 0 or more whitespace
    >         # the end of the tag
  }ix;

Now you can match the <img> tags:

  $HTML =~ s/($tag)/.../g;

However, you only want to match tags without ALT attributes, so...

  my $attr = qr{
    \s*       # 0 or more whitespace
    (?!alt)   # as long as we're not about to match "alt"...
    \w+       # the attribute
    (?:
      \s*     # 0 or more whitespace
      =       # an '='
      \s*     # 0 or more whitespace
      $value  # its value
    )?        # but that part's optional
  }xi;

Now you can do:

  $HTML =~ s/($tag)/add_alt($1)/eg;

  sub add_alt {
    my $tag = shift;
    substr($tag, -1, 0, ' alt="FOO!"');  # insert alt attr.
    return $tag;
  }    

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734

Reply via email to