Replaces 'match' through the correct replacement string.
(self, match)
| 79 | self._expression = expression |
| 80 | |
| 81 | def replace(self, match): |
| 82 | """Replaces 'match' through the correct replacement string.""" |
| 83 | transformed = self._expression |
| 84 | # Replace all $? with capture groups |
| 85 | transformed = _DOLLAR.subn(lambda m: match.group(int(m.group(1))), transformed)[ |
| 86 | 0 |
| 87 | ] |
| 88 | |
| 89 | # Shelter escaped backslashes (\\) so they are not consumed by |
| 90 | # case-switch regexes (\l, \u, \U, \L) or whitespace escapes |
| 91 | # (\n, \t, ...). Restored before the final unescape() which |
| 92 | # turns \\ into a literal \. GH #998, GH #1495. |
| 93 | _ESCAPED_BSLASH = "\x00" |
| 94 | transformed = transformed.replace("\\\\", _ESCAPED_BSLASH) |
| 95 | |
| 96 | # Replace Case switches |
| 97 | def _one_char_case_change(match): |
| 98 | """Replaces one character case changes.""" |
| 99 | if match.group(1)[0] == "u": |
| 100 | return match.group(1)[-1].upper() |
| 101 | return match.group(1)[-1].lower() |
| 102 | |
| 103 | transformed = _ONE_CHAR_CASE_SWITCH.subn(_one_char_case_change, transformed)[0] |
| 104 | |
| 105 | def _multi_char_case_change(match): |
| 106 | """Replaces multi character case changes.""" |
| 107 | if match.group(1)[0] == "U": |
| 108 | return match.group(1)[1:].upper() |
| 109 | return match.group(1)[1:].lower() |
| 110 | |
| 111 | transformed = _LONG_CASEFOLDINGS.subn(_multi_char_case_change, transformed)[0] |
| 112 | transformed = _replace_conditional(match, transformed) |
| 113 | |
| 114 | transformed = fill_in_whitespace(transformed) |
| 115 | transformed = transformed.replace(_ESCAPED_BSLASH, "\\\\") |
| 116 | return unescape(transformed) |
| 117 | |
| 118 | |
| 119 | # flag used to display only one time the lack of unidecode |
no test coverage detected