def django_url_preprocessor(url, root_url):
    """
    Convert url from the simplified string version for app developers to Django regular expression.

    e.g.:

        '/example/resource/{variable_name}/'
        r'^/example/resource/?P<variable_name>[1-9A-Za-z\\-]+/$'
    """
    DEFAULT_EXPRESSION = '[0-9A-Za-z-]+'
    url_parts = url.split('/')
    django_url_parts = []
    if root_url in url_parts:
        index = url_parts.index(root_url)
        url_parts.pop(index)
    for part in url_parts:
        if '{' in part or '}' in part:
            variable_name = part.replace('{', '').replace('}', '')
            part = '(?P<{0}>{1})'.format(variable_name, DEFAULT_EXPRESSION)
        django_url_parts.append(part)
    django_url_joined = '/'.join(django_url_parts)
    if django_url_joined != '':
        django_url = '^{0}/$'.format(django_url_joined)
    else:
        django_url = '^$'
    return django_url
