Ejemplo n.º 1
0
    def replace(self, match):
        """Replaces 'match' through the correct replacement string."""
        transformed = self._expression
        # Replace all $? with capture groups
        transformed = _DOLLAR.subn(lambda m: match.group(int(m.group(1))),
                                   transformed)[0]

        # Replace Case switches
        def _one_char_case_change(match):
            """Replaces one character case changes."""
            if match.group(1)[0] == "u":
                return match.group(1)[-1].upper()
            else:
                return match.group(1)[-1].lower()

        transformed = _ONE_CHAR_CASE_SWITCH.subn(_one_char_case_change,
                                                 transformed)[0]

        def _multi_char_case_change(match):
            """Replaces multi character case changes."""
            if match.group(1)[0] == "U":
                return match.group(1)[1:].upper()
            else:
                return match.group(1)[1:].lower()

        transformed = _LONG_CASEFOLDINGS.subn(_multi_char_case_change,
                                              transformed)[0]
        transformed = _replace_conditional(match, transformed)
        return unescape(fill_in_whitespace(transformed))
    def replace(self, match):
        """Replaces 'match' through the correct replacement string."""
        transformed = self._expression
        # Replace all $? with capture groups
        transformed = _DOLLAR.subn(
                lambda m: match.group(int(m.group(1))), transformed)[0]

        # Replace Case switches
        def _one_char_case_change(match):
            """Replaces one character case changes."""
            if match.group(1)[0] == 'u':
                return match.group(1)[-1].upper()
            else:
                return match.group(1)[-1].lower()
        transformed = _ONE_CHAR_CASE_SWITCH.subn(
                _one_char_case_change, transformed)[0]

        def _multi_char_case_change(match):
            """Replaces multi character case changes."""
            if match.group(1)[0] == 'U':
                return match.group(1)[1:].upper()
            else:
                return match.group(1)[1:].lower()
        transformed = _LONG_CASEFOLDINGS.subn(
                _multi_char_case_change, transformed)[0]
        transformed = _replace_conditional(match, transformed)
        return unescape(fill_in_whitespace(transformed))