user1054158
user1054158

Reputation:

Retrieve the string used to define a regex pattern

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

Answers (2)

jwodder
jwodder

Reputation: 57590

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.

Upvotes: 0

Markku K.
Markku K.

Reputation: 3908

Yes -- pattern.pattern:

pattern = re.compile(r"\w+")
>>> pattern.pattern
'\\w+'

Upvotes: 1

Related Questions