[Tutor] Regular Expression question

Scott Chapman scott_list@mischko.com
Sun Apr 20 14:31:10 2003


On Friday 18 April 2003 13:40, Danny Yoo wrote:
> > Thanks for the reply.  After seeing these replies, it seems clear that
> > you can use the grouping ()'s for more than just capturing a section for
> > output.
>
> Hi Scott,
>
> Yes, the grouping parentheses are serving two functions: they are defining
> a group for both capturing values and for aggregation.

It appears that the aggregation functions of parenthesis are limited.  This 
does not work:

<html([ \t]*>)|([ \t:].+?>)

string being        what the regex
tested:               caught:

<html blah>         blah>
<html>             <html>
<html:subtype>     :subtype>
<html >            <html >
<html tag1 tag2>    tag1 tag2>
<html><head>       <html>

This doesn't work either:

test(r'<html[([ \t]*)([ \t:].+?)]>')

<html blah>
Traceback (most recent call last):
  File "./testre.py", line 19, in ?
    test(r'<html[([ \t]*)([ \t:].+?)]>')
  File "./testre.py", line 6, in test
    mt = re.search(expr, s)
  File "/usr/lib/python2.2/sre.py", line 137, in search
    return _compile(pattern, flags).search(string)
  File "/usr/lib/python2.2/sre.py", line 228, in _compile
    raise error, v # invalid expression
sre_constants.error: unbalanced parenthesis

Scott