All text from camelCase to SNAKE_CASE
I suggest the following regex approach:
Find What: (\b[a-z]+|\G(?!^))((?:[A-Z]|\d+)[a-z]*)
Replace With: \U\1_\2
Match Case: ON.
This will turn camelCase87LikeThis
words to CAMEL_CASE_87_LIKE_THIS
. If you need to add support for those camel words that start with an uppercase letter, use the following regex modification:
(\G(?!^)|\b[a-zA-Z][a-z]*)([A-Z][a-z]*|\d+)
See the regex demo (also tested in Notepad++). Note the placement of the \G
inside the regex and added A-Z
.
Details:
-
(\b[a-z]+|\G(?!^))
- Group 1 capturing either of the two alternatives:-
\b[a-z]+
- start of a word (\b
is the initial word boundary here) followed with 1+ lowercase ASCII letters -
|
- or -
\G(?!^)
- the end position of the previous successful match
-
-
((?:[A-Z]|\d+)[a-z]*)
- Group 2 capturing:-
(?:[A-Z]|\d+)
- either an uppercase ASCII letter ([A-Z]
) or (|
) 1+ digits (\d+
) -
[a-z]*
- 0+ lowercase ASCII letters.
-
The \U\1_\2
replacement pattern turns all the chars to uppercase with \U
and inserts a _
between the two groups (inserted with \1
and \2
backreferences).
Related videos on Youtube
Dancia
Updated on September 16, 2022Comments
-
Dancia over 1 year
I am trying to do some text manipulations using Notepad++ macros. My last step is converting camelCase strings to SNAKE_CASE. So far no luck. I'm not very familiar with regex so can't write my own solution.
Example text file input:
firstLine(874), secondLine(15), thirdLineOfText87(0x0001);
Desired output:
FIRST_LINE(874), SECOND_LINE(15), THIRD_LINE_OF_TEXT_87(0x0001);
Regex or any plugin is an acceptable answer.
-
Yorga Babuscan about 4 yearsFYI, according to a post at the Notepad++ forum, there are conventions about the case types and its names. So, the correct names for the cases asked are lowerCamelCase and SCREAMING_SNAKE_CASE.
-
-
Dancia almost 7 yearsHow should
find what
look like if camelCases started with upper letter, example: FirstLine(874)? -
Dancia almost 7 yearsFirst approach misses all
_
except last one, and second approach generates fromThirdLineOfText87(0x0001);
->THIRD_LINEOF_TEXT_87(0x0001);
missed second _ -
Wiktor Stribiżew almost 7 yearsYou may use
(\G(?!^)|\b[a-zA-Z][a-z]*)([A-Z][a-z]*|\d+)
, the trick was to put\G
branch as the first alternative in the alternation group and adding support for the uppercase letter at the beginning of the word. -
41686d6564 stands w. Palestine over 5 yearsThis replaces
FfdffDF
withFFDFF_D_F
which, I believe, isn't the desired behavior. Here's a tweaked version that takes into account cases likeSomethingIO
andIOSomething
:(\G(?!^)|\b(?:[A-Z]{2}|[a-zA-Z][a-z]*))(?=[a-zA-Z]{2,}|\d)([A-Z](?:[A-Z]|[a-z]*)|\d+)
. -
ChaseMoskal over 4 yearshmm..
MySuperCoolTest.js
becomesMY_SUPERCoolTest.js
-- i can't get the regex to affect the entire string? -
Wiktor Stribiżew over 4 years@ChaseMoskal The regex in the answer is for strings like
caMelCase
, notCaMelCase
. If you use the regex from my comment you will getMY_SUPER_COOL_TEST.js
(just make sure Match case is on) -
Wiktor Stribiżew about 3 years@MoacirSchmidt Then you probably want
(\G(?!^)|\b[[:alpha:]][[:lower:]]*)([[:upper:]][[:lower:]]*|\d+)|\b([[:upper:]][[:lower:]]*)\b
and replace with\U(?{3}$3:$1_$2)
-
Wiktor Stribiżew about 3 years@MoacirSchmidt Not the one in the previous comment.
-
Moacir Schmidt about 3 yearsOn the link you referenced Pascal is replaced by a single _ (underscore)
-
Wiktor Stribiżew about 3 years@MoacirSchmidt Because regex101.com is not Notepad++.
\U(?{3}$3:$1_$2)
replacement will work in Notepad++.