check object being a compiled regular expression

H

Helmut Jarausch

Hi,
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

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

how to test

"if RX is a compiled regular expression"

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

but

if isinstance(RX,_sre.SRE_Pattern)
and
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
 
D

Diez B. Roggisch

Helmut said:
Hi,
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

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

how to test

"if RX is a compiled regular expression"

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

but

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

Many thanks for a hint,


Just invoke type on a compiled expression and store that:

Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Welcome to rlcompleter2 0.96


Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,134
Latest member
JavierIlif

Latest Threads

Top