score:241

Accepted answer

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.

score: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

score:2

at least at sts (springsource tool suite) groups are numbered starting form 0, so replace string will be

replace: ((typename)$0)

score:5

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

score:7

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


Related Query

More Query from same tag