1

Let's consider the variable pattern = re.compile(r"\w+"). Is there a way to retreive the string r"\w+" used to define pattern ?

2 Answers 2

1

Yes -- pattern.pattern:

pattern = re.compile(r"\w+")
>>> pattern.pattern
'\\w+'
Sign up to request clarification or add additional context in comments.

Comments

0

Well, if we look at the docs, we see that regex objects have the attribute:

regex.pattern

The pattern string from which the RE object was compiled.

1 Comment

You're totally right. I did not look at the right place. I was looking for something like representation rather than this.

Your Answer

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