import re
regex = re.compile(r"(\b(?'first_word'[a-z](?!([[:alnum:]]*_)(?# the word doesn't contain underscores))(?:[a-z0-9]*)))|(\G(?'trailing_word'[A-Z][a-z0-9]*))", flags=re.MULTILINE)
test_str = ("camelCase\n"
"camel\n"
"camelCaseCase\n"
"camel684Case\n"
"ca45melCase\n"
"camelCase6523Case\n"
"camelCaseCaseCaseCaseCase\n"
"camelCCase\n"
"PascalCase\n"
"// some random commentary with Capitalized letters\n"
"camel_Snake_Case\n")
subst = "\\L${first_word}${trailing_word:+_\\L${trailing_word}:}"
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html