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 ).
April 16, 2008 at 6:37 am
dfg
October 21, 2008 at 8:48 am
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
May 15, 2017 at 8:33 pm
/ 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
October 21, 2008 at 2:37 pm
… better is:
replace
(?>!import )java.util.HashMap
with
HashMap
May 15, 2017 at 8:56 pm
I seharced a bunch of sites and this was the best.
January 30, 2009 at 11:14 am
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
January 30, 2009 at 12:01 pm
Thomas,
you might want to use the following terms:
Find: ^(.*) =.*$
Replace With: $1 = getSetting(“$1”);
Greetings
Mario
January 30, 2009 at 12:07 pm
OK I found:
([A-z]*) = PageAr[aTemp][13][\d*]
December 30, 2009 at 9:20 am
Hello there,
Interesting tip to change code case :
CTRL + SHIFT + y
CTRL + SHIFT + x
June 9, 2011 at 7:17 pm
thanks m8!!!
March 29, 2010 at 1:15 am
How do I replace the entire capturing group instead of only the subsequence?
November 18, 2010 at 8:55 pm
I’m not sure in eclipse, but in some regex systems $0 equals the whole capturing group.
December 9, 2011 at 9:11 am
[…] 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. … […]
June 26, 2014 at 7:29 am
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.