0

I've looked over the other threads about camel case, but I couldn't find the answer for this case:

I have -> "AddEEven"

and I want -> "add_e_even"

There are cases when I have -> "AddEven" and I want -> "add_even" (This case I know how to do it) I just need a regular expression that covers both cases.

Any help will be appreciated.

1 Answers1

1

Try this:

def convert(name):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()

See here: Elegant Python function to convert CamelCase to snake_case?

Community
  • 1
  • 1
hanleyhansen
  • 6,304
  • 8
  • 37
  • 73