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. |
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
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);
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...