Python: convert camel case to space delimited using RegEx and taking Acronyms into account

后端 未结 10 1935
逝去的感伤
逝去的感伤 2020-12-29 04:38

I am trying to convert camel case to space separated values using python. For example:

divLineColor -> div Line Color

This lin

10条回答
  •  清酒与你
    2020-12-29 04:57

    This should work with 'divLineColor', 'simpleBigURL', 'OldHTMLFile' and 'SQLServer'.

    label = re.sub(r'((?<=[a-z])[A-Z]|(?

    Explanation:

    label = re.sub(r"""
            (            # start the group
                # alternative 1
            (?<=[a-z])  # current position is preceded by a lower char
                        # (positive lookbehind: does not consume any char)
            [A-Z]       # an upper char
                        #
            |   # or
                # alternative 2
            (?

    If a match is found it is replaced with ' \1', which is a string consisting of a leading blank and the match itself.

    Alternative 1 for a match is an upper character, but only if it is preceded by a lower character. We want to translate abYZ to ab YZ and not to ab Y Z.

    Alternative 2 for a match is an upper character, but only if it is followed by a lower character and not at the start of the string. We want to translate ABCyz to AB Cyz and not to A B Cyz.

提交回复
热议问题