boulderz boulderz - 4 months ago 21
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 match.group()[0] + match.group()[2].upper()


When I call
camelize('sales_proj_3_months_ago')
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?

Answer

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: m.group()[1].upper(), key)
Comments