diff options
| author | Chris Beaven <smileychris@gmail.com> | 2012-01-03 22:49:13 +0000 |
|---|---|---|
| committer | Chris Beaven <smileychris@gmail.com> | 2012-01-03 22:49:13 +0000 |
| commit | e52c52ea13be6002ebfa14b7e17ff3004faaf737 (patch) | |
| tree | 1bb8ee0289d410754b74fc8b3498e1bc78dd1b92 /django/utils/regex_helper.py | |
| parent | e5719b203c5a1ee799cd8cb6100342940e260fd7 (diff) | |
Fixed #17492 -- Allow reversal of named backreferences. Thanks nate_b
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17336 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/regex_helper.py')
| -rw-r--r-- | django/utils/regex_helper.py | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/django/utils/regex_helper.py b/django/utils/regex_helper.py index b11fe960bb..ab101e8c32 100644 --- a/django/utils/regex_helper.py +++ b/django/utils/regex_helper.py @@ -134,18 +134,28 @@ def normalize(pattern): raise ValueError("Non-reversible reg-exp portion: '(?%s'" % ch) else: ch, escaped = pattern_iter.next() - if ch != '<': + if ch not in ('<', '='): raise ValueError("Non-reversible reg-exp portion: '(?P%s'" % ch) # We are in a named capturing group. Extra the name and # then skip to the end. + if ch == '<': + terminal_char = '>' + # We are in a named backreference. + else: + terminal_char = ')' name = [] ch, escaped = pattern_iter.next() - while ch != '>': + while ch != terminal_char: name.append(ch) ch, escaped = pattern_iter.next() param = ''.join(name) - result.append(Group(((u"%%(%s)s" % param), param))) - walk_to_end(ch, pattern_iter) + # Named backreferences have already consumed the + # parenthesis. + if terminal_char != ')': + result.append(Group(((u"%%(%s)s" % param), param))) + walk_to_end(ch, pattern_iter) + else: + result.append(Group(((u"%%(%s)s" % param), None))) elif ch in "*?+{": # Quanitifers affect the previous item in the result list. count, ch = get_quantifier(ch, pattern_iter) |
