Eclipse, regular expression search and replace

前端 未结 5 1711
清酒与你
清酒与你 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 17:44

    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

提交回复
热议问题