in reply to Regex For HTML Image Tags?
Let me explain what I did: I changed your .*? to [^>]+? for you only want to match non-end delimeters for the <img> tag in here, and also there I think it's more sensible to use + than * for there is no special need to catch all an empty tag, and there must at least be a whitespace inbetween :-)$html =~ s/<IMG[^>]+?(?:ALT="([^"]*)"[^>]*)>/[image: "$1"]/sgi;
Secondly, why did you escape the brackets in the alt-tag, and at the end? That doesn't really make sense, for you want the special meaning of it at that point. There is also no need to escape it in the replacement string for they don't have a special meaning there.
And, you need to put brackets with a ? followed around the alt-part for as you already noticed it wouldn't match tags without an alt-tag. I did it with (?: so it won't get stored.
This will produce the following:
If you want to have just plain [image] in the replacement if there is no alt atribute present I guess that wouldn't be possible with a single substitute, but you can still do the following substitution afterwards:<img foo><img alt="bar"> [image: ""][image: "bar"]
HTH & HAND!$html =~ s/\[image: ""\]/[image]/g;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Regex For HTML Image Tags?
by zodiac (Beadle) on Mar 27, 2001 at 12:45 UTC | |
by tilly (Archbishop) on Mar 27, 2001 at 17:13 UTC | |
by jeroenes (Priest) on Mar 27, 2001 at 14:21 UTC | |
by alfie (Pilgrim) on Mar 27, 2001 at 13:22 UTC |