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+

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.

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+'

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

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.

Comments

Your Answer

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.