Fix snake case snippet
This commit is contained in:
@ -5,14 +5,14 @@ tags: string,regexp,intermediate
|
|||||||
|
|
||||||
Converts a string to snake case.
|
Converts a string to snake case.
|
||||||
|
|
||||||
Break the string into words and combine them adding `_-_` as a separator, using a regexp.
|
Break the string into words and combine them adding underscore `_` as a separator, using a regexp.
|
||||||
|
|
||||||
```py
|
```py
|
||||||
import re
|
import re
|
||||||
|
|
||||||
def snake(str):
|
def snake(str):
|
||||||
return '-'.join(re.sub('([A-Z][a-z]+)', r' \1',
|
return '_'.join(re.sub('([A-Z][a-z]+)', r' \1',
|
||||||
re.sub('([A-Z]+)', r' \1', str)).split()).lower()
|
re.sub('([A-Z]+)', r' \1', str)).split()).lower()
|
||||||
```
|
```
|
||||||
|
|
||||||
```py
|
```py
|
||||||
|
|||||||
Reference in New Issue
Block a user