In eclipse, is it possible to use the matched search string as part of the replace string when performing a regular expression search and replace?
Basically, I want to replace all occurrences of
variableName.someMethod()
with:
((TypeName)variableName.someMethod())
Where variableName can be any variable name at all.
In sed I could use something like:
s/[a-zA-Z]+\.someMethod\(\)/((TypeName)&)/g
That is, & represents the matched search string. Is there something similar in Eclipse?
Thanks!
Yes, "( )" captures a group. you can use it again with $i where i is the i'th capture group.
so:
search: (\w+\.someMethod\(\))
replace: ((TypeName)$1)
hint: CTRL + space in the textboxes gives you all kinds of suggestions for regular expression writing. (I'm afraid the down key, to go down the list of suggestions, does delete your input in the textbox. Irritating little bug)
Using ...
search = (^.*import )(.*)(\(.*\):)
replace = $1$2
...replaces ...
from checks import checklist(_list):
...with...
from checks import checklist
Blocks in regex are delineated by parenthesis (which are not preceded by a "\")
(^.*import ) finds "from checks import " and loads it to $1 (eclipse starts counting at 1)
(.*) find the next "everything" until the next encountered "(" and loads it to $2. $2 stops at the "(" because of the next part (see next line below)
(\(.*\):) says "at the first encountered "(" after starting block $2...stop block $2 and start $3. $3 gets loaded with the "('any text'):" or, in the example, the "(_list):"
Then in the replace, just put the $1$2 to replace all three blocks with just the first two.
NomeN has answered correctly, but this answer wouldn't be of much use for beginners like me because we will have another problem to solve and we wouldn't know how to use RegEx in there. So I am adding a bit of explanation to this. The answer is
search: (\w+\.someMethod\(\))
replace: ((TypeName)$1)
Here:
In search:
First and last '(' ')' depicts a group in regex
'\w' depicts words (alphanumeric+underscore)
'+' depicts one or more(ie one or more of alphanumeric+underscore)
'.' is a special character which depicts any character( ie .+ means
one or more of any character). Because this is a special character
to depict a '.' we should give an escape character with it, ie '.'
'someMethod' is given as it is to be searched.
The two parenthesis '(',')' are given along with escape character
because they are special character which are used to depict a group
(we will discuss about group in next point)
In replace:
It is given '((TypeName)$1)', here $1 depicts the
group. That is all the characters that are enclosed within the first
and last parenthesis '(' ,')' in the search field
Also make sure you have checked the 'Regular expression' option in
find an replace box
More information about RegEx can be found in http://regexr.com/.
At least at STS (SpringSource Tool Suite) groups are numbered starting form 0, so replace string will be
replace: ((TypeName)$0)
For someone need a explanation and example of how to use regx on eclipse, hope this will be found useful. Here is my problem.
I want to rename
/download.mp4^lecture_id=271
to
/271.mp4
And there can be many of these.
How you should do this is.
Then hit find/replace button
I was looking for regex for removing empty spaces in blank line. Here is the regex to find :
^\s*\r?\n
and replace with
\n
Just use this in find and replace wizard, it will work 100%.