24

I can write a regular expression in NP++ to find under_score_case. I can use TextFX in NP++ to change case to upper and lower case by highlighting and selecting.

How can I use either NP++'s Find/Replace or TextFX's find and replace to stitch these together and convert under_score_case to camelCase?

I want to learn how to do this in NP++ not use a script.

Sample input:

this is_a_line
some more_data_over_here
whoop de_do_da

Desired output:

this isALine
some moreDataOverHere
whoop deDoDa

The regex to match the underscores would be _([a-z]). The replacement that I think exists, but I cannot find is something like _\toupper\1 .

7

I have a solution that's long and convoluted, but will work in Notepad++. It requires the use of regex, optionally normal search and replace, as well as TextFX.

  1. Add a placeholder character to the front of each word, I chose Z. It probably doesn't have to be alphabetic, but it's easier for the last step. Using regex, search for \<([^ ]*)\> and replace with Z\1.
  2. Replace existing spaces with a unique placeholder sequence. I chose #space#. This can be done with regex, but I prefer using normal or expanded.
  3. Replace underscores with spaces. If there are any underscores that shouldn't be replaced, then a custom regex is probably required. I just did a straight search and replace.
  4. Select all text, and from the TextFX menu, select TextFX Characters -> Proper Case.
  5. Now we need to reverse the first 3 steps. Search for spaces, and replace them with nothing. Then search for your space placeholder sequence, and replace with a space. Finally, using regex, search for \<Z([^ ]*)\> and replace with \1.
5
  • Convoluted, but it works.
    – Freiheit
    Jul 19 '11 at 13:01
  • Notepad++'s failings with regex have caused me to come up with solutions like this before, specifically when I need to find and replace across lines or use special characters, like tab.
    – MBraedley
    Jul 19 '11 at 13:31
  • As an aside, has the project lead on NP++ (Mr. Ho as I recall) made any noises about introducing vim-like replacement support? Any plugins to support that? NP++ and TextFX are really powerful, they just need a bit more to stitch them together.
    – Freiheit
    Jul 19 '11 at 13:33
  • I don't really follow the project, but I imagine that he's constrained by the use of Scintilla.
    – MBraedley
    Jul 19 '11 at 13:39
  • 1
    One caveat: If the second and subsequent character in each word is an uppercase letter, it is made lowercase, e.g. ALL_CAPS becomes allCaps.
    – Gnubie
    Jun 26 '12 at 10:26
20

A simple find/replace will do this in NP++:

Find: [_]{1,1}([a-z])

Replace: \U$1

You need to select the 'Regular expression' radio button in the Replace panel for this to work.

2
  • 5
    Or just _(.) as the search string. Jan 28 '16 at 19:53
  • If you want PascalCase (in one step) you can use Find: (?:^|_)([a-z]) Replace: \U$1$2
    – Rubenisme
    Aug 2 '19 at 12:43
6

I typically use vim myself as an editor. The following regular expression accomplishes what you're trying to do in vim:

%s/_\([a-zA-Z]\)/\u\1/g

From what I can tell (I fooled around with NP++ for a bit), Notepad++ does not understand the uppercase macro \u in Perl Regexp. You may not be able to do this entirely with Notepad++. Hopefully, someone will prove me wrong and make your day.

4
  • Yea I'm stuck in a Windows environment. Copying the data over to a *nix or OS X machine works, but its hardly graceful. I can also cross-train other teammates on NP++.
    – Freiheit
    Jul 18 '11 at 20:10
  • I think the same expression works in eMacs. My eMac knowledge is hazy . . .
    – surfasb
    Jul 18 '11 at 20:27
  • @Freiheit There is a vim for windows; although that won't help with the crosstraining other teammates to NP++ Jul 19 '11 at 0:49
  • This actually seems to work fine in recent versions of Notepad++ (I'm using 6.7.7). Sep 1 '15 at 18:33
3

You can do this with one step in notepad++, which is probably more useful:

Find: ([a-z]+)[_]?([a-z]?)([a-z]+)[_]?([a-z]?)([a-z]+)[_]?([a-z]?)([a-z]+)\.php
Replace: $1\U$2\L$3\U$4\L$5\U$6\L$7

The only issue with this, is that you need to know the max time the under score can be present and how the string ends. In the above example, i'm replacing php file names to camelCase, knowing that the under score cannot be present more than 3 times, less is no problem.

2

My personal favourite is sed. It is lightning fast:

> echo make_me_camel_case_please | sed -e 's/(_[a-z])/\U\1/g' -e 's/_//g'

makeMeCamelCasePlease

You can use the -i option to perform the replace on a file you are editing and N++ should pick up the change.

This will also delete all underscores, as with some above solutions. That can be fixed if this is an issue.

2
  • Convert the snake caseI could do this using Notepad++, following the below steps.

  • Convert the snake case input to all lowercase, if not in lowercase already

  • Open the Replace form (Search->Replace OR Ctrl+H)
  • Check Regular Expression radio button
  • Look for _([a-z]) OR _(.)
  • Replace by \u\1 input
0

I work with Notepad++ 7.3.  I had the same problem and I did the following:

  1. Ctrl+H (show find form)
  2. Check Regular Expression radio button
  3. Look for «_([a-z]+)»
  4. Replace by «\u\1»

And worked!

3
  • So this would change all letters to lowercase, and then capitalize the letters that follow underscores, right?  So We use the bean_counter variable, which was Fred's idea would be changed to we use the beanCounter variable, which was fred's idea (lowercasing the letters that were already capitalized)?  This seems unsatisfactory.
    – Scott
    Apr 26 '17 at 18:39
  • Thank you, scott, I didn't fully understand the idea, so i hope now it will be fine. Apr 27 '17 at 10:00
  • Yes, that’s better.  You could probably simplify it a little: just look for «_([a-z])» (without the +).  Unfortunately, this is now essentially equivalent to several of the previous answers.
    – Scott
    Apr 27 '17 at 10:22
-1

I ran into this problem and found out an excellent solution

  1. Replace all underscores("_") with spaces (" ")

  2. Change the first letter of each word to a capital letter

  3. Replace all spaces (" ") with an empty space ("")

  4. Done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.