Eclipse - find and replace the underscore in variable names

I need refactoring variable names.

variable_foo

to

variableFoo

Can this be done using the eclipse search / replace tool ??

change

My problem not only replaces "_", but also edits the next letter in uppercase.

Is regex possible?

+4
source share
6 answers

These links

http://www.php.net/manual/en/function.preg-replace.php#89364

http://www.blog.highub.com/regular-expression/perl-regex/perl-regex-capitalize-the-first-letter-of-a-word/

seems to indicate that Perl and / or PHP Regex syntax supports the \ u operator, which matches the uppercase following it in the replacement expression - and I think I interpret it as a delay from vi '(UNIX text editor) - not has a clue why it doesn't seem to exist in the more modern and popular regex implementation

So, if you can get your code on a system that supports the corresponding version of vi (or an editor that fully supports this perl syntax), and then figure out how to open your documents in it, how to start -replace search, etc., then you will need to use an expression similar to the following:

/\b(\w+?)_(\w+)/$1\u$2/ 

or

 find: \b(\w+?)_(\w+) replace: $1\u$2 
+3
source

The Eclipse Marketplace has a plugin called AnyEditTools that adds many conversion options, one of which is from / to camelCase and underscore_names ( Alt+Ctrl+K ).

However, the Java editor Eclipse currently (Indigo) does not have the correct navigation in identifiers_with_underscores - it only works for CONSTANT_NAMES_CURRENTLY . (More info here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=67381 )

+3
source

The problem with finding and replacing is that it does not know AST, so even if you can use the link and set it, it will do it in the whole file, not just for variables.

IMO, you better find the variables and rename them using the "rename the variable" refactoring, which will also correctly handle the renaming of the getter / setter (if applicable), etc., rather than relying on a (relatively ignorant) and-replace search.

+1
source

Yes, you can do it like this:

 find : (.*)_(.)(.*) replace : $1\C$2$3 

And that should do the trick

+1
source

If you understand correctly, you want to find any vunderscore followed by a lowercase character, and replace it with the uppercase version of that character, i.e. var _f oo → var F oo, _bar _b az → _bar B az, etc.

Unfortunately, it seems that Eclipse can provide this, because as others have said, you can use Ctrl + H (or Ctrl + F in one file) and regular expressions to find these underscores +, but regular expressions do not provide the ability to convert matches (e.g., lowercase to uppercase).

The best you can do is find these variables using a regular expression and then reorganize them the way you want, for example. search using the following expression: \w_\w , which should find var_foo, bar1_baz but not _bar, mark them and rename them (Alt + LShift + R).

Pay attention to the following: \w_\w can find methods, comments, etc., since the search does not know what it actually found (whether it be a variable, class name, method, etc.).

+1
source

It may not be the answer you are looking for, but for all these search / replace tasks I use Notepad ++. I have code in Eclipse, and bulk search / replace in my source in Notepad ++.

0
source

All Articles