23

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,204
  • 14
  • 51
  • 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
  • 40,566
  • 14
  • 81
  • 139
Bill the Lizard
  • 386,424
  • 207
  • 554
  • 861