Regular Expressions 101

Community Patterns

camelCase2snake_case

3

Regular Expression
PCRE2 (PHP >=7.3)

Description

Convert camelCase to snake_case

def camelCase2snake_case(txt):
    """ 
    Convert camelCase to snake_case 
    https://regex101.com/library/wUcSv4
    """
    txt = re.sub(r'([A-Z]+)', '_\\1', txt.strip())
    txt = txt.lower()
    return txt

In Python 🐍 the \L case modifier in the substitution pattern to lower case is not supported.

By JV-conseil

Submitted by JV-conseil - a year ago