stacker
stacker

Reputation: 68962

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?

Upvotes: 5

Views: 233

Answers (2)

janhink
janhink

Reputation: 5023

I think you need this:

search:

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

replace:

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

Upvotes: 3

BoltClock
BoltClock

Reputation: 723698

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);

Upvotes: 1

Related Questions