faqts : Computers : Programming : Languages : Python : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 5 people (80%) answered Yes
Recently 1 of 2 people (50%) answered Yes

Entry

How can I catch an error in re.compile?
How can I test for a bad regular expression?

Mar 1st, 2000 19:39
Nathan Wallace, unknown unknown, Fredrik Lundh


Why not just compile a bad expression and see what happens? ;-)

>>> re.compile("(")
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "C:\py152\lib\re.py", line 79, in compile
    code=pcre_compile(pattern, flags, groupindex)
pcre.error: ('missing )', 1)

(to catch the exception, use try/except re.error)