Created
January 9, 2014 22:01
-
-
Save cameronp98/8342877 to your computer and use it in GitHub Desktop.
Convert CamelCase to snake_case 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 convert(name): | |
s1 = re.sub(r"(.)([A-Z][a-z]+)", r"\1_\2", name) | |
return re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", s1).lower() | |
tests = ["CamelCase", "CamelCamelCase", "getHTTPResponseCode", | |
"getHTTPResponseCode", "HTTPResponseCodeXYZ"] | |
for test in tests: | |
print(test, "=>", convert(test)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment