![]() |
|
laziness, impatience, and hubris | |
PerlMonks |
Re: Regex For HTML Image Tags?by alfie (Pilgrim) |
on Mar 27, 2001 at 11:51 UTC ( #67454=note: print w/replies, xml ) | Need Help?? |
You need to tweak your regular expression a little bit. Let
me start with this fast diddle:
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 :-) 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: HTH & HAND! -- Alfie
In Section
Seekers of Perl Wisdom
|
|