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
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;
}
}
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)
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.