33

I'd like to go through all of my source code files and replace every occurence of k_Xyyy with k_xyyy (switch the first letter after k_ from uppercase to lowercase).

I'm using the eclipse dialog to search and replace multiple files. Right now I have the regex \bk_([A-Z]).

How do I specify the replacement string of the regex?

ErikE
  • 48,881
  • 23
  • 151
  • 196

4 Answers4

16

I just resolved the same task (had to turn .net interface into java interface) utilizing the power of VIM :)

void DoMethod1 -> void doMethod1
Foo PerformMethod2 -> Foo performMethod2
:%s/\(^\s*\w\+\s\+\)\([A-Z]\)/\1\L\2/g

Here we are searching for (optional indentation followed by return type followed by whitespace) followed by (Uppercase letter). Braces are capturing groups. Then we are performing a replacement first capturing group \1 lowercase \L second capturing group \2.

This of course requires you to open file in Vim, but anyway this is much faster then doing the same thing by hand in Eclipse.

edio
  • 652
  • 1
  • 8
  • 21
12

That is not possible. Either use Eclipse's re-factoring functionality, or replace them one at a time:

regex       : \bk_A
replacement : k_a 

regex       : \bk_B
replacement : k_b 

...

regex       : \bk_Z
replacement : k_z 
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
1

I needed to do this for a huge chunk of source code where string literals needed to be converted to lowercase. I found a way using Notepad++ and the Python Script plugin, as used here.

Community
  • 1
  • 1
Nick
  • 4,901
  • 40
  • 61
  • 8
    in Notepad++ you can just use `\L$1\E` in the replacement string to convert the first matching group to lowercase letters. The same works with \U...\E for uppercase – fuemf5 Jul 24 '14 at 08:04
0

(for me, since I just started programming, this was more fun to think about) Take $pattern_to_change and convert it from ascii to decimal using ord().Take the resulting dec number and add 32. Then convert $desired_pattern back to ascii using chr().

Or just download SublimeText and use its Find and Replace feature to Find All occurrences and replace them with difference text (Sublime has regex as well).

I'm sure you could have converted one billion by hand since this post is like 5 years old, but you could have complete this in 5 minutes with Sublime.

Really useful text editor.

Andrew
  • 15,357
  • 6
  • 66
  • 101
les
  • 564
  • 7
  • 19