136

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!

Neuron
  • 5,141
  • 5
  • 38
  • 59
  • Good documentation on Eclipse RegEx: http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jubula.client.ua.help%2Fhtml%2Freference%2Fnode4.html – Kellen Stuart Sep 15 '16 at 19:59

5 Answers5

249

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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
NomeN
  • 17,140
  • 7
  • 32
  • 33
  • That's a nice answer and a useful hint. It would have been more helpful had it been described in little more detail. – iammilind May 24 '13 at 16:58
  • Bravo Eclipse, you implemented this feature properly! – Kenogu Labz Jul 17 '13 at 19:30
  • I especially love the example for "Hex code for Unicode character" :) – Stewart Murrie Jan 01 '14 at 21:00
  • I would like to add some code that really helped me. I needed to replace every `stringVar.equals("xxx")` with `"xxx".equals(stringVar)`. I used the following regex in the find/replace dialog: `search: ([^()! ]+).equals\("(.*)"\)`, `replace: "$2".equals\($1\)`. – LaDude Nov 13 '14 at 09:25
  • Not for me. Always replaces the first group...? – RightmireM Jan 15 '15 at 08:21
  • do you know if it is possible to use transform options in replace to make upper or lower replaces? hypothetical example: replace: ((TypeName)TOUPPER($1)) – tierrarara Feb 19 '20 at 19:18
8

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.



Screenshot

RightmireM
  • 2,381
  • 2
  • 24
  • 42
6

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

Neuron
  • 5,141
  • 5
  • 38
  • 59
padippist
  • 1,178
  • 1
  • 16
  • 30
2

At least at STS (SpringSource Tool Suite) groups are numbered starting form 0, so replace string will be

replace: ((TypeName)$0)
  • 5
    That's not true afaik. The $0 represents the whole original search term. In this case I guess that would work coincidentally though. – Walter Heck Nov 28 '11 at 11:32
0

For someone who needs an explanation and an example of how to use a regxp in Eclipse. Here is my example illustrating the problem.

enter image description here

I want to rename

/download.mp4^lecture_id=271

to

/271.mp4

And there can be multiple of these.

Here is how it should be done.

enter image description here

Then hit find/replace button

OpalApps
  • 149
  • 13
MadNik
  • 7,713
  • 2
  • 37
  • 39