def easy(url_str): import re patt = re.compile(r':\w+/?') matches = patt.findall(url_str) for match in matches: var = match[1:-1] var_re = r'(?P<%s>.*)/' % var url_str = url_str.replace(match, var_re) url_str += '$' return url_str # usage: # urlpatterns = patterns('', # url(easy('calendar/:year/:month/'), 'apps.events.views.calendar'), # ) # output: # urlpatterns = patterns('', # url(r'^calendar/(?P.*)/(?P.*)$', 'apps.events.views.calendar'), # ) # so much win !