[Tutor] Can you see my error? [regex is buggy, avoid it!]
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sun, 5 Aug 2001 02:03:35 -0700 (PDT)
On Sun, 5 Aug 2001, Danny Yoo wrote:
> Hmmm....
>
> ###
> >>> import regex
> __main__:1: DeprecationWarning: the regex module is deprecated; please use
> the re module
> >>> # yes, yes, I know... but let me try this...
> >>> regex.compile('\)')
[Note: I should have been more careful by using raw strings:
regex.compile(r'\)')
But the test case still stands. *grin*]
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> regex.error: Badly placed parenthesis
> ###
>
> There's the little critter!
I forgot to complete my thoughts on this. Here goes...
...There's the little critter! ... And that's DEFINITELY a bug in regex:
quoted right parens should be a perfectly fine regular expression. I
guess that's why regex is deprecated. *grin*
The easy fix is to use 're' instead: it has the same interface as regex,
and doesn't have the bug that you're running into:
###
>>> rparen_re = re.compile(r"\)")
>>> rparen_re.search("I don't have an rparen")
>>> rparen_re.search("But (I) do")
<re.MatchObject instance at 80d7f48>
###
So 're' does the right thing in terms of handling quoted rparens. That
was an unexpected bug! I'm pretty sure that regex's brokenness is what
was causing the problem. Switch over to 're', and your program should be
fine.
My curiosity is satisfied... and now I can sleep... *grin*