Jeff,
Thanks so much for sharing that regex! You've not only solved my problem
but given me a lot of new tools to work with!
I found the use of ! to tell Perl not to find <string> particularly useful
btw.
Thanks again!
--
Noah Sussman
Senior Web Developer
Deltathree, The IP Communications Network
75 Broad St, 31st Floor
New York, NY 10004
tel 212-500-4845
fax 212-500-4888
[EMAIL PROTECTED]
www.deltathree.com
"Moving Heaven and Earth without effort is simply a matter of
concentration."
‹ Hagakure
> From: Jeff Pinyan <[EMAIL PROTECTED]>
> Reply-To: [EMAIL PROTECTED]
> Date: Mon, 14 May 2001 22:43:46 -0400 (EDT)
> To: Noah Sussman <[EMAIL PROTECTED]>
> Cc: [EMAIL PROTECTED]
> Subject: Re: negative matching?
>
> 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
>