24

Assume I have created a compiled re:

x = re.compile('^\d+$')

Is there a way to extract the pattern string (^\d+$) back from the x?

Bartosz Radaczyński
  • 18,396
  • 14
  • 54
  • 61
  • this is a duplicate of http://stackoverflow.com/questions/189861/what-property-returns-the-regular-expression-used-when-recompile-was-called (although this one is stated more nicely) –  Oct 10 '08 at 13:06
  • possible duplicate of [how can i obtain pattern string from compiled regexp pattern in python](http://stackoverflow.com/questions/1415924/how-can-i-obtain-pattern-string-from-compiled-regexp-pattern-in-python) - while this Q here was earlier, the later one has answers with more info, including Py3 – cfi Oct 18 '13 at 23:20
  • Possible duplicate of [What property returns the regular expression used when re.compile was called?](https://stackoverflow.com/questions/189861/what-property-returns-the-regular-expression-used-when-re-compile-was-called) – fabrik Sep 18 '18 at 10:33

1 Answers1

31

You can get it back with

x.pattern

from the Python documentation on Regular Expression Objects

Thanatos
  • 42,585
  • 14
  • 91
  • 146
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880