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

后端 未结 10 1940
逝去的感伤
逝去的感伤 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:58

    I couldnt get a really nice regex, but this worked decently.

    ([a-z]+)([A-Z][a-z]+)?([A-Z][a-z]+)?([A-Z][a-z]+)?([A-Z][a-z]+)?

    Breaking it down it is:

    ([a-z]+) Any series of lowercase characters

    ([A-Z][a-z]+)? Any uppercase character followed by 1 or more lowercase characters. This is optional

    Then I repeated the second group 4 times. This will only work if you dont have any more than 4 "sections" or uppercase characters. Add or take away that regex grouping as necessary. It will work if there less than this number (i.e. it will work on divLineColor) This will not match on words that are all uppercase.

提交回复
热议问题