Is there an indicated way in Python 3 to copy compiled regular expressions? The following code throws a TypeError
(same with deepcopy
), and I am not sure where to go from there.
import re
import copy
p = re.compile( r'foo' )
copy.copy(p) # TypeError: cannot copy this pattern object
I have thought of creating a copy manually with:
p_copy = re.compile(p.pattern, p.flags)
but I wonder if this is correct for all possible regexes; if it were that simple, then why would the reference library not implement copy like this?