Adam R. Grey
Adam R. Grey

Reputation: 2024

replace a capture group followed by a number

In visual studio's find and replace, I'm trying to replace (P|Q)1 with the p or the Q followed by a 2. So P1 with P2 and Q1 with Q2. however, $12 seems to look for capture group 12 so it just shows up as literally $12. Any character I put between the 1 and the 2, including backslash, shows up. Can this be done?

Upvotes: 2

Views: 168

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627126

You can safely use

([PQ])1

and replace with ${1}2 (since {} around the group ID makes the ID unambiguous) even in VS2012.

If you are unsure of what is going on with numbered backreferences, you can always use named captures:

(?<val>[PQ])1

and replace with ${val}2.

enter image description here

Upvotes: 2

Related Questions