473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 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 2475
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Dan S | last post by:
My application asks the user to enter in a date - in the mm/dd/yyyy format. Is there any quick and easy way to verify the date the user enters is formatted correctly? Right now I'm calling...
6
by: Matt | last post by:
I want to check if the user enters alphabet or numbers only in the text box. If the user enters non-alphabet or non-numbers, I should pop up a message and doesn't allow the user to do that. I am...
8
by: Merlin | last post by:
Hi there, I would like to add a plausability check for numbers with JS, but regex is not really a strong knowledge of mine. Can anybody give me a hint, or lead me to the right script for doing...
0
by: Hugo Wetterberg | last post by:
Hi, I had a problem with a rather complex regex expression that didn't return the same results in 1.1 and 2.0. I first thought that I could make a utility assambly that referenced the 1.1 version...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
3
by: shahargs | last post by:
Hi, I'm looking for the best way to check if regular expression return true (it's mean - there is a match). for example, i want "if" that check if this regular expression: .*born.*to.* has a...
4
by: bizt | last post by:
Hi, Im looking for a regular expression to check that a entered filename is valid. I have a cms Im building where the user can enter the name of a file, enter content and then click Save where...
6
by: better_cs_now | last post by:
Hello all, class Foo {/* Details don't matter */}; class Bar { public: Bar(): m_Foo(/* Construct a Foo however it wants to be constructed */); const Foo &GetFoo() const { return m_Foo; }...
15
by: puzzlecracker | last post by:
What is the quickest way to check that the following: const line; only contains whitespace, in which case to ignore it. something along these lines: isspacedLine(line); Thanks
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.