Eclipse, regular expression search and replace

user21037 picture user21037 · Sep 3, 2009 · Viewed 103.6k times · Source

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!

Answer

NomeN picture NomeN · Sep 3, 2009

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.