Skip to content

Instantly share code, notes, and snippets.

@PattoMotto
Last active September 27, 2018 06:59
Show Gist options
  • Save PattoMotto/cb1b8cea01e0a407d30dd725f456df74 to your computer and use it in GitHub Desktop.
Save PattoMotto/cb1b8cea01e0a407d30dd725f456df74 to your computer and use it in GitHub Desktop.
# credit https://dev.to/rrampage/snake-case-to-camel-case-and-back-using-regular-expressions-and-python-m9j
# credit https://stackoverflow.com/a/3847369
import re
REG = r"(.+?)([A-Z])"
def camel(match):
return match.group(1).lower() + match.group(2)[0].upper() + match.group(2)[1:].lower()
lowerFirstChar = lambda s: s[:1].lower() + s[1:] if s else ''
words = """MyClass
MyClassFactory
MyClassFactoryBuilder
MyClassFactoryBuilderImpl
myInstance
myInstance2
abc
patternMatcher
Hello""".splitlines()
results = [re.sub(REG, camel, lowerFirstChar(w.strip()), 0) for w in words]
[print(w) for w in results]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment