On 4-7-2012 3:03, Tim Chase wrote: > r = re.compile(r""" > < # tag opening > \s* # optional whitespace > img # the tag > \b # must end here > (?: # one of these things: > \s+ # whitespace > (?:[a-z][a-z0-9]+:)? # an optional namespace > src # a "src" attribute > \s* # optional whitespace > = # the equals sign > \s* # optional whitespace > ( # capture the value > "[^"]*" # a double-quoted string > | # or > '[^']*' # a single-quoted string > | # or > [^-a-z0-9._:]*" # per HTML spec > ) # end of the captured src > | # or something that's not src > \s+ # whitespace > (?:[a-z][a-z0-9]+:)? # an optional namespace > [a-z0-9]+ # the tag name > (?: # an optional value > \s* # optional whitespace > = # an assignment > \s* # optional whitespace > (?: # the value > "[^"]*" # a double-quoted string > | # or > '[^']*' # a single-quoted string > | # or > [^-a-z0-9._:]*" # per HTML spec > ) # end of the captured src > ) # end of ignored attribute > )* # zero or more attributes > \s* # optional whitespace > (?:/\s*) # an optional self-closing > > # closing > > """, re.I | re.VERBOSE) > > So, that said, I'm not sure it's much more complex to use a real > parsing library :-)
Aside from the \b matching positive against ><, this is a syntax validator and of course when validating syntax you'll use a validating parser. For all other cases, you'd be more interested in "image tags with a src attribute", making the use-case quite a bit more simple. My main beef with modern software is that for the simplest of things one flees to full-blown libraries which happen to provide some utilities, but the other 98% of the code from that library is unused. Case in point, PIL to verify if a file is an image. But my rant alarm went off. :) >> It's a trade-off you should make a decision on, not just blatantly >> dismiss regular expressions when a document contains tags or call them >> complex when they contain more then two characters. > > I'm not blithely dismissing them, But the author I replied to did. I think you and I are on the same page. Either way, the OP now has some nice examples of how to /refine/ regular expressions and that's the real craft :). -- Melvyn Sopacua -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

