I would like to convert camelCase to snake_case, but ignoring multiple capitalized letters in the camelCase.
I have the following function:
re.sub(r'(?<!^)(?=[A-Z])', '_', name).upper()
This returns the following results:
abcFunction --> ABC_FUNCTION
ABCFunction --> A_B_C_FUNCTION
Is there a way to get the following:
abcFunction --> ABC_FUNCTION
ABCFunction --> ABC_FUNCTION
>Solution :
import re
name = 'abcFunction'
name2 = 'ABCFunction'
def converter(name):
name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).upper()
print(converter(name))
print(converter(name2))
Output
ABC_FUNCTION
ABC_FUNCTION