Reputation:
Let's consider the variable pattern = re.compile(r"\w+")
. Is there a way to retreive the string r"\w+"
used to define pattern
?
Upvotes: 1
Views: 73
Reputation: 57590
Well, if we look at the docs, we see that regex objects have the attribute:
regex
.patternThe pattern string from which the RE object was compiled.
Upvotes: 0
Reputation: 3908
Yes -- pattern.pattern:
pattern = re.compile(r"\w+")
>>> pattern.pattern
'\\w+'
Upvotes: 1