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.
M flag is used as an argument inside the regex method to perform a match inside a multiline block of text. Note: This flag is used with metacharacter ^ and $ . When this flag is specified, the pattern character ^ matches at the beginning of the string and each newline's start ( \n ).
The g flag indicates that the regular expression should be tested against all possible matches in a string. Each call to exec() will update its lastIndex property, so that the next call to exec() will start at the next character.
The pattern rules for glob are not regular expressions. Instead, they follow standard Unix path expansion rules. There are only a few special characters: two different wild-cards, and character ranges are supported.
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.compile
to a proxy function that passes re.DOTALL
.
import re
re.my_compile = re.compile
re.compile = lambda pattern, flags: re.my_compile(pattern, flags | re.DOTALL)
and this change will happen to everybody else.
You can even package this up in a context manager, as follows:
from contextlib import contextmanager
@contextmanager
def flag_regexen(flag):
import re
re.my_compile = re.compile
re.compile = lambda pattern, flags: re.my_compile(pattern, flags | flag)
yield
re.compile = re.my_compile
and then
with flag_regexen(re.DOTALL):
<do stuff with all regexes DOTALLed>
All of the flags can be set in the regular expression itself:
r"(?s)Your.*regex.*here"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With