MCPcopy
hub / github.com/django/django / simplify_regex

Function simplify_regex

django/contrib/admindocs/views.py:511–523  ·  view source on GitHub ↗

r""" Clean up urlpattern regexes into something more readable by humans. For example, turn "^(?P<sport_slug>\w+)/athletes/(?P<athlete_slug>\w+)/$" into "/<sport_slug>/athletes/<athlete_slug>/".

(pattern)

Source from the content-addressed store, hash-verified

509
510
511def simplify_regex(pattern):
512 r"""
513 Clean up urlpattern regexes into something more readable by humans. For
514 example, turn "^(?P<sport_slug>\w+)/athletes/(?P<athlete_slug>\w+)/$"
515 into "/<sport_slug>/athletes/<athlete_slug>/".
516 """
517 pattern = remove_non_capturing_groups(pattern)
518 pattern = replace_named_groups(pattern)
519 pattern = replace_unnamed_groups(pattern)
520 pattern = replace_metacharacters(pattern)
521 if not pattern.startswith("/"):
522 pattern = "/" + pattern
523 return pattern

Callers 2

test_simplify_regexMethod · 0.90
get_context_dataMethod · 0.85

Calls 4

replace_named_groupsFunction · 0.90
replace_unnamed_groupsFunction · 0.90
replace_metacharactersFunction · 0.90

Tested by 1

test_simplify_regexMethod · 0.72