How to print SRE_Pattern (regexp object) text for debugging purposes?

D

dmtr

I need to print the regexp pattern text (SRE_Pattern object ) for
debugging purposes, is there any way to do it gracefully? I've came up
with the following hack, but it is rather crude... Is there an
official way to get the regexp pattern text?
"cre\n_compile\np0\n(S'^abc$'\np1\nI2\ntp2\nRp3\n."

-- Cheers, Dmitry
 
M

MRAB

dmtr said:
I need to print the regexp pattern text (SRE_Pattern object ) for
debugging purposes, is there any way to do it gracefully? I've came up
with the following hack, but it is rather crude... Is there an
official way to get the regexp pattern text?
2
 
D

dmtr

 >>> import re
 >>> r = re.compile('^abc$', re.I)
 >>> r.pattern
'^abc$'
 >>> r.flags
2


Hey, thanks. It works.

Couldn't find it in a reference somehow.
And it's not in the inspect.getmembers(r).
Must be doing something wrong.....

-- Cheers, Dmitry
 
M

MRAB

dmtr said:
Hey, thanks. It works.

Couldn't find it in a reference somehow.
And it's not in the inspect.getmembers(r).
Must be doing something wrong.....
Occasionally you'll find classes whose instances don't reveal all their
attributes to dir() and so forth, so it's always a good idea to
double-check the documentation.

The documentation mentions the .pattern and .flags attributes at:

http://docs.python.org/library/re.html
Section 7.2.4. Regular Expression Objects

(Python 2.6)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top