Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat text matched by a regex?

I'm trying to add log4j to a legacy software using eclipse search/replace.

The idea is to find all class declarations and replace them by, the declaration itself plus the definition of the logger in the next line.

search

".*class ([A-Z][a-z]+).*\{"

replace:

"final static Logger log = Logger.getLogger($1.class);"

How can I prepend the matched pattern (the class definition) to the replace string?

like image 845
stacker Avatar asked Oct 11 '22 08:10

stacker


2 Answers

I think you need this:

search:

(.*class ([A-Z][a-z]+).*\{)

replace:

$1\Rfinal static Logger log = Logger.getLogger($2.class);
like image 77
janhink Avatar answered Oct 26 '22 22:10

janhink


You can always capture the whole thing and put it in. The inner capture group lives in a second backreference.

Find:

(.*class ([A-Z][a-z]+).*\{)

Replace with:

$1 final static Logger log = Logger.getLogger($2.class);

like image 35
BoltClock Avatar answered Oct 26 '22 23:10

BoltClock