Fix snake case snippet

This commit is contained in:
rfezzani
2019-09-30 23:15:00 +02:00
parent 158878552a
commit 2b70917df8

View File

@ -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