Eclipse Find/Replace


This is a quick example on how to use the power of find and replace in eclipse. Some might say that eclipse find and replace is not as powerful as something like vi or sed but it is still rather powerful.



I had a program where I wanted to transform all of all of parameters created using #define statement to command line arguments so that I could write an algorithm to test the effect of different values for these parameters. 

First copy and past all of the define statements to another location of your program ( I wanted to keep the define statements to be used as defaults if no new parameter value is passed.

After you have copied the text should look something like 


#define PED_BRAKING_RATE    0.95f

#define PED_COMFORT_ZONE    1.5f

#define PED_QUERY_RADIUS    10.0f
 
NOTE: Make sure you have Regular expressions check-box checked.

To replace these lines with atribute definitions you can use ability of find and replace to capture strings (). Putting a regular expressions between () will save that string into a variable \(1 that can be used in your replace with:. IN this case use "#define ([A-Z|_]*)[ |.|0-9|f]*" (don't include the quotes ") to find one of the above lines. Now this line can be replaced with "float $1;". very cool stuff.<br /><br />If you want to change all of the character to lowercase you will have to use ctrl+shift+y. Highlight want you want to change then use ctrl+shift+y.<br /><br />The last thing I did was copy and paste a bunch of lines that assigned the #defined macro to the class varaible<br /><br />ped_max_speed = PED_MAX_SPEED;<br /><br />That I had to do slightly by hand because find and replace does not replace with lowercase as far as I know.<br /><br />I want to change these lines into the piece of code that will be used to check if there was a command line parameter option passed to change this value. If there was update the value. This part was really easy.<br /><br />I could use this in my find statement:<br /><br /><pre class="prettyprint"><code ><br />([a-z|_]*)[a-z| |_|A-Z|=]*;<br /></code></pre><br /><br />and this to replace each line with the code I wanted:<br /><pre class="prettyprint"><code class="language-py"><br />else if ((*optionIter).first == "\)1")\n\t\t{\n\t\t\t(optionIter).second >> $1;\n\t\t}


To result in:


        else if ((
optionIter).first == "ped_max_speed")
        {
            (*optionIter).second >> ped_max_speed;

        }

I used this to replace about 50 assignments. Took me about 30 seconds. 

Now all of my #define statement parameters are also command line arguments that can be passed to easily test out different values.

Happy hunting!

References:
  1. Me.