Regular Expression question

Rob Wolfe blue99 at interia.pl
Mon Aug 21 06:07:56 EDT 2006


stevebread at yahoo.com wrote:
> Hi, I am having some difficulty trying to create a regular expression.
>
> Consider:
>
> <tag1 name="john"/>  <br/> <tag2 value="adj__tall__"/>
> <tag1 name="joe"/>
> <tag1 name="jack"/>
> <tag2 value="adj__short__"/>
>
> Whenever a tag1 is followed by a tag 2, I want to retrieve the values
> of the tag1:name and tag2:value attributes. So my end result here
> should be
> john, tall
> jack, short
>
> My low quality regexp
> re.compile('tag1.+?name="(.+?)".*?(?!tag1).*?="adj__(.*?)__',
> re.DOTALL)
>
> cannot handle the case where there is a tag1 that is not followed by a
> tag2. findall returns
> john, tall
> joe, short
>
> Ideas?

Have you tried this:

'tag1.+?name="(.+?)".*?(?=tag2).*?="adj__(.*?)__'

?

HTH,
Rob




More information about the Python-list mailing list