Simplify tagre regex.

This commit is contained in:
Bastian Kleineidam 2012-10-12 21:47:41 +02:00
parent 9c032c9006
commit b3e51ddc93

View file

@ -42,6 +42,10 @@ def tagre(tag, attribute, value, quote='"', before="", after=""):
@return: the generated regular expression suitable for re.compile() @return: the generated regular expression suitable for re.compile()
@rtype: string @rtype: string
""" """
if before:
before += "[^>]*"
if after:
after += "[^>]*"
attrs = dict( attrs = dict(
tag=case_insensitive_re(tag), tag=case_insensitive_re(tag),
attribute=case_insensitive_re(attribute), attribute=case_insensitive_re(attribute),
@ -50,7 +54,7 @@ def tagre(tag, attribute, value, quote='"', before="", after=""):
before=before, before=before,
after=after, after=after,
) )
return r'<\s*%(tag)s\s+(?:[^>]*%(before)s[^>]*\s+)?%(attribute)s\s*=\s*%(quote)s%(value)s%(quote)s[^>]*%(after)s[^>]*>' % attrs return r'<\s*%(tag)s\s+(?:[^>]*%(before)s\s+)?%(attribute)s\s*=\s*%(quote)s%(value)s%(quote)s[^>]*%(after)s>' % attrs
def case_insensitive_re(name): def case_insensitive_re(name):