boulderz boulderz - 3 months ago 10x
Python Question

Python regular expression sub

I am using this sub:

def camelize(key):
print re.sub(r"[a-z0-9]_[a-z0-9]", underscoreToCamel, key)

Which calls this function

def underscoreToCamel(match):
return[0] +[2].upper()

When I call
it returns 'salesProj3_monthsAgo' instead of 'salesProj3MonthsAgo.' However, if I call `camelize('sales_proj_30_days_ago') it returns 'salesProj30DaysAgo' as expected.

So there is a problem with my regex substitution when there is only one character in between underscores. How can I write my regex substitution to account for these cases?


You could use look-behind so that each match does not overlap with the previous one.

def camelize(key):
    return re.sub('(?<=[a-z0-9])_[a-z0-9]', lambda m:[1].upper(), key)