472,119 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

check object being a compiled regular expression

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
Mar 25 '06 #1
2 2424
This is crude, but works:
import re
RX= re.compile('^something')
str(RX).find("<_sre.SRE_Pattern") == 0

True

Mar 25 '06 #2
Helmut Jarausch schrieb:
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
for nice experiences hit <tab> multiple times
import re
re_type = type(re.compile("foo"))
isinstance(re.compile("bar"), re_type) True

Diez
Mar 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Dan S | last post: by
8 posts views Thread by Merlin | last post: by
reply views Thread by Hugo Wetterberg | last post: by
25 posts views Thread by Mike | last post: by
3 posts views Thread by shahargs | last post: by
6 posts views Thread by better_cs_now | last post: by
15 posts views Thread by puzzlecracker | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.