With the code I’m using (r' ', ' '), changes to a a a a(r' a', a' a'), when it should change to (r' a', ' '),. What’s a more natural way to do this? How do I do this with re.sub?
Current code, see here
for key, value in newgroupdict.items():
try:
newstr = newstr.replace(re.search(e, line).group(key), value)
except:
pass
Examples:
Expression: \s*(?:url)?\(r?["|'](?P<pattern>[^'"]+)["|'],\s*["|']?direct_to_template["|']?,\s*{["|']template["|']:\s*["|'](?P<template>[^'"]+)["|']}\),
String: (r'^$', direct_to_template, {'template': 'home.html'}),
Dictionary: {u'pattern': u'^$abc', u'type': u'direct to template', u'template': u'home.html'}
Output: (r'^$abc', direct_to_template, {'template': 'home.html'}),
Expression: \s*(?:url)?\(r?["|'](?P<pattern>[^'"]+)["|'],\s*["|']?(?P<view>[^'"]+)["|']?\),
String: (r'^urls/', 'urls.views.urls'),
Dictionary: {u'pattern': u'^new_urls_pattern/', u'type': u'view', u'view': u'urls.views.urls'}
Output: (r'^new_urls_patterns/', 'urls.views.urls'),
================= Incorrect Output ========================
Expression: \s*(?:url)?\(r?["|'](?P<pattern>[^'"]+)["|'],\s*["|']?(?P<view>[^'"]+)["|']?\),
String: (r'^urls/', 'urls'),
Dictionary: {u'pattern': u'^new_urls_pattern/', u'type': u'view', u'view': u'urlsxyz'}
Incorrect Output: (r'^urlsxyz/', 'urlsxyz'),
Correct Output: (r'^urls/', 'urlsxyz'),

There are many ways to achieve this with a regexp, here’s one:
I’m not a good teacher and regexps are tricky to understand, still I’ll try to break this down to you:
'will match a'in your string subject,(opens group\1, anything found between this and)will be in group\1,[^']matches any character except',+means that the previous character class ([^']) can be repeated,'will match the''to compensate for the replaced'in 1.,\1, everything from 2. to 4., all non'characters,'to compensate for the last'that is in the pattern regexp,Feel free to experiment with it, remove the count argument and stuff like that. But you will have to learn regexp at some point, so you should see that as a great opportunity to read the holy manual. Knowing to regexp will make you a much better programmer and give you power over text data.