diff --git a/snippets/camel.md b/snippets/camel.md index a989e8fc8..f11f41507 100644 --- a/snippets/camel.md +++ b/snippets/camel.md @@ -5,13 +5,15 @@ tags: string,regexp,intermediate Converts a string to camelcase. -Break the string into words and combine them capitalizing the first letter of each word, using a regexp, `title()` and `lower`. +Use `re.sub()` to replace any `-`,`_` or ` ` (space) with a space, using the regexp `r"(_|-)+"`. +Use `title()` to capitalize the first letter of each word convert the rest to lowercase. +Finally, use `replace()` to remove spaces between words. ```py import re def camel(s): - s = re.sub(r"(\s|_|-)+", " ", s).title().replace(" ", "") + s = re.sub(r"(_|-)+", " ", s).title().replace(" ", "") return s[0].lower() + s[1:] ```