Jimmy.D
Jimmy.D

Reputation: 99

How to replace with a capture of the first regex on intelliJ?

I would like to know if it was possible in "IntelliJ" to replace (CTRL + SHIFT + R) with the capture of the regex that allows to find the occurrences.

enter image description here

My regex: \b(\w+)\s?:\s?+\1\b\s?[,}]

I can't find myself any information about it.

My objective is to replace:

model: model,

by

model,

Thank you, Jimmy.

Upvotes: 2

Views: 507

Answers (1)

The fourth bird
The fourth bird

Reputation: 163237

In your pattern you are already referring to the first capturing group using a backreference and you could also use that in the replacement.

To keep the second part, one option is to use a second capturing group and in the replacement refer to those capturing groups using $1$2

\b(\w+)\s?:\s?+\1\b\s?([,}])
                      ^^^^^^

Regex demo

Upvotes: 2

Related Questions