diff --git a/README.md b/README.md index efede5aae..89f452c2a 100644 --- a/README.md +++ b/README.md @@ -1838,16 +1838,15 @@ palindrome('taco cat') # True 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 `_` as a separator, using a regexp. ```py 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.replace('-', ' '))).split()).lower() ```
diff --git a/snippet_data/snippetList.json b/snippet_data/snippetList.json index a5bedb2f3..036f90d9f 100644 --- a/snippet_data/snippetList.json +++ b/snippet_data/snippetList.json @@ -935,7 +935,7 @@ "type": "snippetListing", "title": "snake", "attributes": { - "text": "Converts a string to snake case.\n\nBreak the string into words and combine them adding `_-_` as a separator, using a regexp.\n\n", + "text": "Converts a string to snake case.\n\nBreak the string into words and combine them adding `_` as a separator, using a regexp.\n\n", "tags": [ "string", "regexp", @@ -943,7 +943,7 @@ ] }, "meta": { - "hash": "4217b274beb495919f1aa33afe8e910d04776c35685f01ba4b3fcdd0b176e6e1" + "hash": "50ee46a9c0ed48161f20e555da66c06561abe3d9188b5a84d9eda25de594f87f" } }, { diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json index f6cedee0d..80e6bf29b 100644 --- a/snippet_data/snippets.json +++ b/snippet_data/snippets.json @@ -1236,9 +1236,9 @@ "type": "snippet", "attributes": { "fileName": "snake.md", - "text": "Converts a string to snake case.\n\nBreak the string into words and combine them adding `_-_` as a separator, using a regexp.\n\n", + "text": "Converts a string to snake case.\n\nBreak the string into words and combine them adding `_` as a separator, using a regexp.\n\n", "codeBlocks": { - "code": "import re\n\ndef snake(str):\n return re.sub(r\"(\\s|_|-)+\",\"_\",\n re.sub(r\"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+\",\n lambda mo: mo.group(0).lower(),str)\n )", + "code": "import re\n\ndef snake(str):\n return '_'.join(re.sub('([A-Z][a-z]+)', r' \\1',\n re.sub('([A-Z]+)', r' \\1',\n str.replace('-', ' '))).split()).lower()", "example": "snake('camelCase') # 'camel_case'\nsnake('some text') # 'some_text'\nsnake('some-mixed_string With spaces_underscores-and-hyphens') # 'some_mixed_string_with_spaces_underscores_and_hyphens'\nsnake('AllThe-small Things') # \"all_the_smal_things\"" }, "tags": [ @@ -1248,7 +1248,7 @@ ] }, "meta": { - "hash": "4217b274beb495919f1aa33afe8e910d04776c35685f01ba4b3fcdd0b176e6e1" + "hash": "50ee46a9c0ed48161f20e555da66c06561abe3d9188b5a84d9eda25de594f87f" } }, {