Advanced search/replace technique using regex and capture groups

Let's imaging you are working on some legacy Java project and stumbled upon this test class:

public void test_Sum_Operator() {
  ...
}

public void test_Div_Operator() {
  ...
}

public void test_Mod_Operator() {
  ...
}

Tests are correct, but the naming convention is off here so you decided to change the method using camel case insted.

Doing it by hand is possible, but if there were hundreds of methods it could become a tedious task.

One way of doing it is by using search and replace functionality and a litle bit of regex magic.

By looking at the method names we see the pattern here: test_%{operator name}%_ Operator  So the search string will be like this: /test_\w+_Operator/ The only problem left is that replace string is changing dynamically based on the operator name: for div it's going to be testDivOperator, for mod – testModOperator .

In the majority of IDE, it's possible to get the capture group value of the matched regex by accessing $1 (1st group), $2 (2nd group) and so on.  

Having this in mind we can write the following regex insted: /test_(\w+)_Operator/ and replace it with: test$1Operator

As you've already guessed it's not limited to just one capture group, so by combining $1, $2, $3 you may come up with pretty advanced replacement logic.