NetBeans IDE

NetBeans Search & Replace with RegExp


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 input…

keyword-call.html
keyword-component.html
keyword-custom.html
keyword-description.html
keyword-documentation.html
keyword-in.html
keyword-inout.html
keyword-methods.html

The output…(note that the text in bold above needs to be extracted as highlighted in the xml below)

nb_regexp_replace_example_hl

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.

nb_edit_search_dlg

Find: ^keyword-(.*)\.html
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.

Advertisements

3 thoughts on “NetBeans Search & Replace with RegExp

  1. btw in my case I left out the caret at the start because I have white space before the search result, and I want to preserve the white space

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s