I am trying to change settings.py by regex instead of by hand. I have a regex that matches all of installed apps and grabs all except the last paren:
rgx = "(?P<start>INSTALLED_APPS.*?)\)"
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
)
match = re.search(rgx, content, re.DOTALL)
grabs
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
but for some reason when I go to sub it, with a new app, like
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'my-new-app',
text, n = re.subn(rgx, new_string, content, re.DOTALL)
it doesn't sub anything. I ran as subn and proved it wasn't matching. This makes no sense, because I am using the exact same regex that just made a match. How can I get the exact same regex to match and then not match the exact same string?
答案 0 :(得分:1)
您使用re.DOTALL
作为count
参数。请将其用于flags
:
re.subn(rgx, new_string, content, flags=re.DOTALL)