Merge pull request #148 from rohitbabugaddeti/patch-3
modified regexp and documentation
This commit is contained in:
@ -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:]
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user