From f9d820201b105b27d894a5436ef24894d3339d4e Mon Sep 17 00:00:00 2001 From: Gaddeti Rohit Babu <30732419+rohitbabugaddeti@users.noreply.github.com> Date: Fri, 18 Oct 2019 15:16:19 +0530 Subject: [PATCH 1/2] modified regexp and documentation I think there is no use of using space literal match (/s) in the pattern. So I removed it and updated the snippet and it's document accordingly. Please review it and let me know if I am wrong. --- snippets/camel.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/camel.md b/snippets/camel.md index a989e8fc8..5e2383b03 100644 --- a/snippets/camel.md +++ b/snippets/camel.md @@ -5,13 +5,13 @@ tags: string,regexp,intermediate Converts a string to camelcase. -Break the string into words and combine them capitalizing the first letter of each word, using a regexp, `title()` and `lower`. +Identify one or more groups containing the pattern with `-` or `_` using regexp `r"(_|-)+"` and replace the pattern with a `" "` (space literal) using `re.sub` method. Apply `title()` method on the obtained string to Capitalize the starting letter and lower the other letters of every word in the string. Finally, remove the spaces in the string using `replace()` method ```py import re def camel(s): - s = re.sub(r"(\s|_|-)+", " ", s).title().replace(" ", "") + s = re.sub(r"(_|-)+", " ", s).title().replace(" ", "") return s[0].lower() + s[1:] ``` From af91bb16955dcc3b0344dc7c4dccf201f25c5f02 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 31 Oct 2019 09:36:39 +0200 Subject: [PATCH 2/2] Update camel.md --- snippets/camel.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/snippets/camel.md b/snippets/camel.md index 5e2383b03..f11f41507 100644 --- a/snippets/camel.md +++ b/snippets/camel.md @@ -5,7 +5,9 @@ tags: string,regexp,intermediate Converts a string to camelcase. -Identify one or more groups containing the pattern with `-` or `_` using regexp `r"(_|-)+"` and replace the pattern with a `" "` (space literal) using `re.sub` method. Apply `title()` method on the obtained string to Capitalize the starting letter and lower the other letters of every word in the string. Finally, remove the spaces in the string using `replace()` method +Use `re.sub()` to replace any `-`,`_` or ` ` (space) with a space, using the regexp `r"(_|-)+"`. +Use `title()` to capitalize the first letter of each word convert the rest to lowercase. +Finally, use `replace()` to remove spaces between words. ```py import re