Monday, September 26, 2011

Using Regular Expressions in Eclipse

Regular Expressions (regex for short) are a great way to manipulate text with repeatable patterns that is not possible with simple search/replace. For instance you may want to search all public, protected and private and methods. You might then want to narrow the search by looking at only methods that take more than 1 argument, etc. Once you perfect your search regex, you might want to replace what you found with some other data you find during search. I'm going to assume a basic understanding in regular expression. If you are not familiar with it, a good reference for Regular Expressions is http://java.sun.com/developer/technicalArticles/releases/1.4regex

To start simple, lets search for any of the following key words; public, protected or private in the simple class shown below.
public class RegExTest {
public void voidMethod() {
}

public int intMethod() {
return 1;
}

protected String hello() {
return "Hello";
}

protected String greetUser(String name) {
return hello() + name;
}

private double doubleMethod() {
return 1.0;
}
}

If you didnt have regular expressions, you would first have to search for public, then do a 2nd search for protected and a 3rd search for private. Using regular expressions, you can search for any of the 3 using at once a single regex.

The regex you want to use is the following as shown in the figure below.  Make sure that you selected the Regular Expressions check box in the Search/Replace dialog.
(public|protected|private)


In regex terms, what this means is to find any one of the three items mentioned within the parenthesis that are seperated by |

Now that we did our first search using regular expressions, lets now try to replace what was found with the regex and replace what was found with "final" prepended to it, i.e. if we find "public", it becomes "final public", if we find "protected", it becomes "final protected", etc.

To do this we need to come up with a string to replace the text found. In regex terms, what is within a pair of parenthesis becomes a "capture group" that you can use for later substitutions using $n format where n is a number specifying the group. In our case, the group number is 1, since we have only one group, namely the parenthesis around (public|protected|private). So using final $1 as our replace string, gives us what was desired, as shown below.