Two RE proposals

David LeBlanc whisper at oz.net
Fri Jul 26 22:53:11 EDT 2002


<snip>
>     >> The * doesn't (and shouldn't) operate over grouping parens.  You're
>     >> asking it to supply you with a variable number of groups, which it
>     >> can't do.
>
>     David> You're right - it doesn't operate over grouping parens, but why
>     David> _shouldn't_ it? IIRC, _some_ regex pacakges could do this...
>
> How about using non-grouping parens:
>
>     >>> pat = re.compile(r"((?:a|b)*)")
>     >>> pat.match("ababaaaabccdabab")
>     <_sre.SRE_Match object at 0x40348ea0>
>     >>> _.group(1)
>     'ababaaaab'
>
> Skip

Thanks for the pointer on locals()... another nice Python feature :-)

Here's a practical example of what I have in mind:
str = "<tag attr1='a' attr2='b' attr3='c'/>"
attr = r"\s*\w+\s*=\s*['\"].*?['\"]"
tag = r"<tag(!<attr>)*\s*/>"
tagpat = re.compile(tag)
tagmat = tagpat.match(tagpat, str)
for attr in tagmat.group(1):
	print attr
>> attr1='a'
>> attr2='b'
>> attr3='c'

If you're the owner of the re module and are willing to provide mentoring,
I'm willing to add these.

Dave LeBlanc
Seattle, WA USA





More information about the Python-list mailing list