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