Skip to content

Instantly share code, notes, and snippets.

@PattoMotto
Created September 26, 2018 10:49
Show Gist options
  • Save PattoMotto/d0d9cfd8aba5e0a9743a723228fb760d to your computer and use it in GitHub Desktop.
Save PattoMotto/d0d9cfd8aba5e0a9743a723228fb760d 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
import re
REG = r"(.+?)([A-Z])"
def snake(match):
return match.group(1).lower() + "-" + match.group(2).lower()
words = """MyClass
MyClassFactory
MyClassFactoryBuilder
MyClassFactoryBuilderImpl
myInstance
myInstance2
abc
patternMatcher
Hello""".splitlines()
results = [re.sub(REG, snake, w.strip(), 0).lower() 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