Tuesday, July 3, 2012

Re: Use regular expression to retrieve all image tags from a given content

On 07/03/12 12:57, Melvyn Sopacua wrote:
> On 30-6-2012 15:23, Sunny Nanda wrote:
> What you're looking for is:
> prog = re.compile(r'<img.*?/>')
> matches = re.search(prog)
> for match in matches :
> print match
>
>> On a sidenote, you should not be using regular expressions if you are doing
>> anything complex that what you are doing right now.
>
> This isn't complex. The email validator in django is complex. Using an
> XML parser for this is quite overkill. If you need several elements
> based on their nesting and/or sister elements, then an XML parser makes
> more sense, or better xpath queries. This is simple stuff for regular
> expressions and what they're made for.

The reason for using a true parser is to avoid obscure edge cases.
Your example fails on both

<IMG ... >

and

< img ... >

Also, depending on the use-case (such as stripping them out of
validated code), a use-case such as

<i<img>mg src="evil.gif">

could get part stripped out and leave the evil <img> tag in the text.

-tkc



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment