From e52c52ea13be6002ebfa14b7e17ff3004faaf737 Mon Sep 17 00:00:00 2001 From: Chris Beaven Date: Tue, 3 Jan 2012 22:49:13 +0000 Subject: 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 --- django/utils/regex_helper.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'django/utils') 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) -- cgit v1.3