Some useful Regular Expressions for Find/Replace in Eclipse IDE

Regular expressions is powerful tool mostly used in search, edit and manipulate text. A regular expression define a search pattern for strings. The abbreviation for regular expression is "regex".Regular expressions are used in several programming languages. But, unfortunately each language / program supports regex slightly different.

In this blog, I am going to explain the use of Regular Expression(regex) in Eclipse IDE. There are eleven examples in total.



Eclipse supports regular expressions within the dialog ‘Find/Replace’ (CTRL+F) and from Search->Search(CTRL+H). Don't forget to check the Regular Expression check box.



NOTE: while finding the regex pattern you must not forget to escape the non-printing characters, symbols etc. The backslash \ is an escape character. 


Pattern 1 : Finding myVar1 , myVar2, myVar3 etc

SearchString :: myVar(1|2|3) OR  myVar[1|2|3]


Pattern 2 : Finding myVar1x,  myVar2y , myVar3z , myVar1z etc
SearchString :myVar(1|2|3)(x|y|z) OR  myVar[1|2|3][x|y|z]


Pattern 3 : Finding myVar1, myVar2, myVar3ab, myVar5 etc. but not myVar4
SearchString :: myVar[^4]


Pattern 4 : Finding myVar1, myVar2, myVar4 etc but not myVar3ab
SearchString :: myVar[1|2|3|4]     (with a space after ] )


Pattern 5:  Finding some character at end of line
e.g. : finding   null); at the end such as :
doSth(str, null);
callFunction(param1,null);
SearchString :: null\)\;$ 

NOTE: regex$Finds regex match at the end of the line
Pattern 6 : finding the patterns like
textArea.setText("jpt");
textArea2.setText("jptsdfsdf");
textArea2.setText("jptsdfsdf");

SearchString :: \S*\.setText\S*


NOTE: \S represents a non-whitespace character, and \S* means  any length of some non-whitespace character. Here \S* is required  before and after .setText 


Pattern 7 : finding the comments like (with single word)
//this
//is
//comment
SearchString :: \/\/[\S]*$


Pattern 8 : finding blank lines 
SearchString : ^\s*\r?\n



Pattern 9 : finding blank lines  and remove it

SearchString :: ^\s*\r?\n

Replace with ::         <--EMPTY 





Pattern 10 : finding a group and replace
To extract parts of a string that have been matched using the grouping metacharacters, use the special variables $1, $2, etc.
TO FIND : property.someMethod()
TO REPLACE WITH : ((Object)property.someMethod());

In this case we should find "property.someMethod()" and append "((Object)" at first and ");" at end.
SearchString : (property.someMethod\(\))

Replace With : (Object)$1);




Pattern 11 : More complex finding and replacing a group 
TO FIND: 
value[0] = 100;
value[1] = 131;

value[2] = 102; 

value[3] = 123; 


....

Desired Output:
value[0] = getValue(0,100);

value[1] = getValue(1,131);

value[2] = getValue(2,102); 

value[3] = getValue(3,123); 
...


Here we should find two groups $1 = 0,1,2,3 ... $2= 100,101,102,...
SearchString :  (\d+)\] = (\d+);
Replace With :  $1] = getValue($1,$2);


NOTE: Do not forget the spaces in "[0] =100;"


No comments :

Post a Comment

Your Comment and Question will help to make this blog better...