Eclipse, regular expression search and replace

前端 未结 5 1681
清酒与你
清酒与你 2020-11-30 17:39

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

5条回答
  •  北海茫月
    2020-11-30 18:00

    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/.

提交回复
热议问题