Last active
June 15, 2016 20:08
-
-
Save mmas/1534f22e093dc93af906134e61d61241 to your computer and use it in GitHub Desktop.
Camelcased and dashed text to underscored text in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def underscorize(x): | |
""" | |
Camelcased and dashed text to underscored text. | |
Examples: | |
>>> underscorize('CamelCasedText') | |
'camel_cased_text' | |
>>> underscorize('camel-cased-text') | |
'camel_cased_text' | |
>>> underscorize('Camel-Cased-Text') | |
'camel_cased_text' | |
""" | |
return re.sub(r'([^_])([A-Z])', r'\1_\2', re.sub(r'-', '_', x)).lower() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment