Would it be possible to define a global flag so that Python’s re.compile() automatically sets it ? For instance I want to use re.DOTALL flag for all my RegExp in — say — a class?
It may sound weird at first, but I’m not really in control of this part of the code since it’s generated by YAPPS. I just give YAPPS a string containing a RegExp and it calls re.compile(). Alas, I need to use it in re.DOTALL mode.
A quick fix would be to edit the generated parser and add the proper option. But I still hope there’s another and more automated way to do this.
EDIT:
Python allows you to set flags with the (?…) construct, so in my case re.DOTALL is (?s). Though usefull, it doesn’t apply on a whole class, or on a file.
So my question still holds.
Yes, you can change it to be globally
re.DOTALL. But you shouldn’t. Global settings are a bad idea at the best of times — this could cause any Python code run by the same instance of Python to break.So, don’t do this:
The way you can change it is to use the fact that the Python interpreter caches modules per instance, so that if somebody else imports the same module they get the object to which you also have access. So you could rebind
re.compileto a proxy function that passesre.DOTALL.and this change will happen to everybody else.
You can even package this up in a context manager, as follows:
and then