0

How do you extract the original 'raw' string from a Python regex. For example, I have the following simple regex:

import re
test_line_re = re.compile(r'Test \d+ Result: \s+')

I want to be able to print: Test \d+ Result \s+

Rod
56.5k3 gold badges43 silver badges55 bronze badges
asked Sep 1, 2011 at 3:02

4 Answers 4

3

You can use the pattern attribute:

print test_line_re.pattern

You should always search through the documentation when you have questions like this.

jfs
417k211 gold badges1k silver badges1.7k bronze badges
answered Sep 1, 2011 at 3:05
Sign up to request clarification or add additional context in comments.

Comments

0
>>> re.compile(r'Test \d+ Result: \s+').pattern
'Test \\d+ Result: \\s+'
answered Sep 1, 2011 at 3:05

Comments

0

Is there any reason you can't store the string before compiling the expression? i.e.

import re
pattern = r'Test \d+ Result: \s+'
test_line_re = re.compile(pattern)
print pattern
answered Sep 1, 2011 at 4:10

Comments

0

re.compile is not very useful. It usually best just to keep the pattern the whole time anyhow. You can get the pattern from the pattern attribute, but if possible just don't ever manually compile it.

answered Sep 1, 2011 at 19:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.