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.
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
import re
def snake(str):
return '-'.join(re.sub('([A-Z][a-z]+)', r' \1',
re.sub('([A-Z]+)', r' \1', str)).split()).lower()
return '_'.join(re.sub('([A-Z][a-z]+)', r' \1',
re.sub('([A-Z]+)', r' \1', str)).split()).lower()
```
```py