Eclipse is so feature-packed that it is often easy to miss stand-out functions, and the use of regular expressions for efficient search and replace is something I find to be often overlooked. If you’ve ever found yourself hitting the arrow keys to iterate row after row making the same minor change, this tip is for you.
Capture Groups & Groups
If you’re not too familiar with regular expressions then I would suggest reading my brief introduction here. For now we can get by with understanding the concepts of capture groups and groups. Capture groups define what is being searched for, whilst groups are the blocks of text found. The former are defined by wrapping the regular expression in parenthesis and the latter by a reference from one upward that is preceded by a string. Like most things in software, it’s all best explained with an example.
Applying Regular Expressions
Let’s take a simple block of code:
Map<String, Integer> values = new HashMap<>(); values.put("A", 0); values.put("B", 1); values.put("C", 2); // line after line of this stuff...
If for whatever reason we decide to make the value of the
Long, the code will no longer compile:
Map<String, Long> values = new HashMap<>(); values.put("A", 0); values.put("B", 1); values.put("C", 2); // line after line of this stuff...
The first thing we need to do is determine the replacement expression. In this case it is pretty simple – we just want to append the character “L” to each numeric value to describe them as
Long. In this case it’s easy. We want to find each single numeric digit, and then let Eclipse know it’s a group by wrapping the expression in parenthesis:
The second thing we need to do is tell Eclipse that we want the value found to be used as part of the replacement, otherwise we’d remove the number entirely during replace. As this is the first capture group, it is identified by the number one. We want that value in place with an L following:
To put this in action, bring “Find/Replace” dialogue as normal, set the “Regular expressions” option box and enter the values:
With that done, hitting “Replace All” will make the magic happen:
Map<String, Long> values = new HashMap<>(); values.put("A", 0L); values.put("B", 1L); values.put("C", 2L); // line after line of this stuff...
We’re not limited to single value replacement, being able to capture and use as many groups as we need. Let’s consider another example where a list of teams needs to have some text values both prepended and inserted:
List<String> teams = new ArrayList<>(); teams.add("Group1AA"); teams.add("Group2AB"); teams.add("Group3AC"); teams.add("Group4AD"); teams.add("Group5AE");
Were going to take the group name and prepend it with the value DEV-, then add an underscore hyphen the two character code at the end. To do this, we’ll define two capture groups as two regular expressions within parenthesis:
Now we can reference each group with our changes included:
Here $1 relates to (Group[0-9]), and $2 corresponds with (\w+). Putting these values into our regular-expression “Find/Replace” dialogue will make the required changes:
List<String> teams = new ArrayList<>(); teams.add("DEV-Group1_AA"); teams.add("DEV-Group2_AB"); teams.add("DEV-Group3_AC"); teams.add("DEV-Group4_AD"); teams.add("DEV-Group5_AE");
It can be difficult to remember all of the regular expressions that might be used, and pressing CTRL+F in the “Find” box provides a list of the most commonly used:
With only a basic understanding of regular expressions Eclipse can take a lot of the drudge work away from us when making numerous changes to large files. I find these techniques particularly useful on such thing as input files used for unit tests, where hundreds of such changes may be required. I’ll return to this topic and offer some more detailed examples for later, but for now I hope you’ve discovered something useful.