check object being a compiled regular expression

Helmut Jarausch jarausch at
Sat Mar 25 16:58:09 CET 2006

sorry, this seems to be a FAQ but I couldn't find anything

I need to check if an object is a compiled regular expression

import re
RX= re.compile('^something')

how to test

"if RX is a compiled regular expression"

type(RX)  says
<type '_sre.SRE_Pattern'>


if isinstance(RX,_sre.SRE_Pattern)
if isinstance(RX,re._sre.SRE_Pattern)
both fail.

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

More information about the Python-list mailing list