Wednesday August 30, 2006 Yay, 100 NetBeans IDE hacks! Well, maybe not exactly 100, but almost. :-) I collected them over the last couple of months, mostly from blogs, but also from the IDE's help system. Since the file was growing too long, I split them up into four files that will be published one after the other in the coming weeks. Here is my favorite one for this week.
While trying out various hacks, I experimented (among other things) with regular expressions. I like to use them in Linux, but the NetBeans Editor's search&replace form only supports them in the search pattern, not in the replace string. Or so I thought, because I learned that the replace string does already support back-references! What's that? Something very useful -- let me give you an example:
Say, you have lots and lots of variable declarations:
... public int x; public int y; public int z;
But then you notice you need them all to be private and initialized to zero instead:
... private int x = 0; private int y = 0; private int z = 0;
Obviously, you cannot use basic Search&Replace to replace them in one step -- due to the unique variable names, you'd have to replace private by public first, and then you'd have to replace all the semicolons by = 0;. This is just a simple example, it could be more complex and involve more steps...
Well, this is where back-reference syntax comes in handy. You need to check the 'Regular Expression' box in the Search&Replace dialog to make it work.
Basically, each substring in the search term that is surrounded by brackets is 'copied' into a clipboard.
From there it can be 'pasted' into the replace term with a certain symbol.
To paste the first bracketed substring
into the replace term, you write the symbol $1 into the replace term. To paste the second one, you write $2 -- and so on. The symbols are a reference back to the bracketed expression.
How does it work in an actual example?
Well, variable names are alphanumeric strings;
as a regular expression, an alphanumeric string is expressed as \w+. (The \w stands for any member of the set of alphanumeric characters, while the + sign makes it a sequence.)
Knowing this, you can capture the variable names from the search term
with the bracketed (\w+) symbol,
and restore them in the replace term by calling the back-reference $1 symbol.
Find What: public int (\w+); Replace With: private int $1 = 0;
See?
Remember you can use $2, $3, etc,
for the following bracketed substrings if you need to reference more than one expression at once.
To give you one more example: The symbol $0 stands for the whole search term.
Let's say you want to close each <img ... > tag in an HTML file
by replacing it with <img ... />. You can't just search for > and
replace it by /> -- because you have hundreds of other tags that you don't want to modify... You want to make sure to only add the backslash to img tags.
In regular expressions, the [^] symbol
is used for negation, so [^>]+ matches "a sequence of not >", that is, a string made up of any kind of character but a closing tag bracket. So this following search pattern matches a string starting with <img including whatever arguments, and excluding its closing tag bracket. The whole caboodle can be back-referenced with $0 in the replace term, where you now insert the /:
Find What: <img [^>]+ Replace With: $0 /
The closing tag bracket > stays where it is, you just insert the backslash right before it.
Well, those are just silly examples I came up with, but I think you see how powerful these patterns are for more complex queries. Jan Goyvaerts wrote a Regular Expression Quickstart -- if you wanna learn more about regular expressions in general. Once you get the hang of it, they are addictive, believe me.
PS: Uh before I forget it -- the NetBeans IDE hacks pages will have submit forms, and of course you can contribute your own hacks, or just tell us which si your favorite one...! They will be living documents, and they will grow over time as new hacks are discovered.
Posted by seapegasus ( Aug 30 2006, 09:52:29 PM CEST ) Permalink Comments [2]