Nono.MA

Convert from Snake to Came case with Regular Expressions in Python

MAY 12, 2022

I recently used this regular expression to convert a string from snake to camel case.

import re

def camel(match):
    return match.group(1) + match.group(2).upper()

regex = r"(.*?)_([a-zA-Z])"
snake_case = "my_camel_case_variable_here"
camel_case = re.sub(regex, camel, snake_case, 0)
# returns myCamelCaseVariableHere

BlogCodePython