NetBeans has a pretty powerful (and useful) search and replace feature that comes in handy when you want to manipulate a lot of text within one or many files. It’s power is magnified even more with superb regular expression support both for searching and replacing.
Recently I needed to create some xml tags using the text given below.
The output…(note that the text in bold above needs to be extracted as highlighted in the xml below)
This can be done easily by selecting “Edit/Replace” in NetBeans top menu and then providing the following regular expressions for the “Find What” and “Replace With” fields as shown below.
Replace: <tocitem text=”$1″ target=”com.abc.webservices.model.keywords.keyword-$1″>
Note how the relevant segment (highlighted in the xml sample) is extracted using parenthesis ( ) in the search expression which is then referenced using $1 in the replace expression. This can be done even when you want to capture multiple segments. In such situations the replace expression can use $2, $3,… etc.
Another excellent example of this functionality has been blogged here.