search and replace with regular expressions in eclipse

eclipse supports regular expressions within his dialog ‘search and replace’ (ctrl-f). you only have to activate the corresponding checkbox ‘Regular Expressions’ in the options pane.

let’s see eclipse in action by a simple example. suppose you want to surround a couple of numbers with some text:

100 should be replaced by
INSERT INTO kredit( pk_id, fk_sicherheit ) VALUES ( 1, 100 );
200
should be replaced by
INSERT INTO kredit( pk_id, fk_sicherheit ) VALUES ( 1, 200 );

300
should be replaced by
INSERT INTO kredit( pk_id, fk_sicherheit ) VALUES ( 1, 300 );
and so on …

in most regular expression dialects you can refer to a piece of chars which are surrounded by parenthesis with the $-operator. this is also true in eclipse. with this in mind, the task is easy. now you can search for the numbers using

^([0-9]+)$

see the parenthesis around the number expression? now we can refer to the matching in the replacement:

INSERT INTO kredit( pk_id, fk_sicherheit ) VALUES ( 1, $1 );

of course you could reference some more subexpressions (also surrounded by parenthesis) by incrementing the reference counter (as you know, the order of the starting parenthesis within the expression is relevant to the reference counter – $1, $2, $3 and so on ).

Advertisements

14 Responses to “search and replace with regular expressions in eclipse”

  1. dhe Says:

    Another example: if you want to change all (e.g.) java.util.HashMap references to HashMap (but not the imports, of course) you can search for ([^t][^ ])java.util.HashMap and replace it with $1HashMap

    • Florence Says:

      / Acho que alguém vai perder o braço. As pessoas usam a religião para justificar atos como esses, mas nenhuma religião mata pessoas. As pessoas matam umas às outras. O resto é só desculpa esersaapada.Gortfi deste comentário ou não: 0

  2. dhe Says:

    … better is:
    replace
    (?>!import )java.util.HashMap
    with
    HashMap

  3. Thomas Says:

    I am very bad in reg exp, I try to replace:

    SearchSO = PageAr[aTemp][13][38];
    SearchWidth = PageAr[aTemp][13][39];
    SearchHeight = PageAr[aTemp][13][40];

    By

    SearchSO = getSetting(“SearchSO”);
    SearchWidth = getSetting(“SearchWidth “);
    SearchHeight = getSetting(“SearchHeight “);

    * = PageAr[aTemp][13][\d]; by $1 = getSetting(“$1); doesnt work ;-( Can u help me? Cheers

  4. Mario Gleichmann Says:

    Thomas,

    you might want to use the following terms:

    Find: ^(.*) =.*$
    Replace With: $1 = getSetting(“$1”);

    Greetings

    Mario

  5. Thomas Says:

    OK I found:

    ([A-z]*) = PageAr[aTemp][13][\d*]

  6. Djos Says:

    Hello there,
    Interesting tip to change code case :
    CTRL + SHIFT + y
    CTRL + SHIFT + x

  7. Arunabh Das Says:

    How do I replace the entire capturing group instead of only the subsequence?

  8. Regex eclipse | Edebat Says:

    […] search and replace with regular expressions in eclipse « brain …Nov 21, 2007 … eclipse supports regular expressions within his dialog ’search and … the corresponding checkbox ‘Regular Expressions’ in the options pane. … […]

  9. real estate north Says:

    Simply want to say your article is as astonishing. The clarity in your post is simply
    excellent and i can assume you’re an expert on this subject.

    Fine with your permission allow me to grab your RSS feed to
    keep updated with forthcoming post. Thanks a million and please continue the enjoyable work.


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

%d bloggers like this: