From 158878552a5d17ab567ae055f368489c499b59c2 Mon Sep 17 00:00:00 2001 From: Riadh Date: Mon, 30 Sep 2019 16:12:09 +0200 Subject: [PATCH] Fix snake snippet --- snippets/snake.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/snippets/snake.md b/snippets/snake.md index efb49396e..480d87fc7 100644 --- a/snippets/snake.md +++ b/snippets/snake.md @@ -11,15 +11,13 @@ Break the string into words and combine them adding `_-_` as a separator, using import re def snake(str): - return re.sub(r"(\s|_|-)+","-", - re.sub(r"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+", - lambda mo: mo.group(0).lower(),str) - ) + return '-'.join(re.sub('([A-Z][a-z]+)', r' \1', + re.sub('([A-Z]+)', r' \1', str)).split()).lower() ``` ```py -snake('camelCase'); # 'camel_case' -snake('some text'); # 'some_text' -snake('some-mixed_string With spaces_underscores-and-hyphens'); # 'some_mixed_string_with_spaces_underscores_and_hyphens' -snake('AllThe-small Things'); # "all_the_smal_things" +snake('camelCase'); # 'camel-case' +snake('some text'); # 'some-text' +snake('some-mixed_string With spaces_underscores-and-hyphens'); # 'some-mixed-string-with-spaces-underscores-and-hyphens' +snake('AllThe-small Things'); # "all-the-smal-things" ```