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
You can use the pattern attribute:
print test_line_re.pattern
You should always search through the documentation when you have questions like this.
Comments
>>> re.compile(r'Test \d+ Result: \s+').pattern
'Test \\d+ Result: \\s+'
Comments
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
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.