Search-and-Replace RegEx Capture Group with a Number

2

I'm trying to use the Visual Studio editors Regular Expressions to find and replace text using capture-groups, but have run into a problem.

I'm trying to find and capture a set of 5 Alpha-Numerics:

(\w{5})

And search-replace that group to append a "1" after it:

$11

Here I really mean $1 The Captured Group + 1 Text to append

Examples:

 227TW ==> 227TW1
 1053X ==> 1053X1

However, it obviously interprets $11 as "Capture Group Eleven".

How can I properly make the Search/Replace understand that $1 and 1 are separate elements?

Things I've tried, that failed:

$1(1)  : 227TW ==> 227TW(1)
$1\1   : 227TW ==> 227TW\1
$1 1   : 227TW ==> 227TW 1
$1^1   : 227TW ==> 227TW^1
($1)1  : 227TW ==> (227TW)1

abelenky

Posted 2019-02-04T21:12:06.413

Reputation: 771

Answers

4

Turns out the right answer is:

${1}1

The curly-braces around the number identify the capture group without confusing it with the next digit.

abelenky

Posted 2019-02-04T21:12:06.413

Reputation: 771

1great answer!! Actually I wanted to suggest to step approach, e.g. first replace to $1##1## or whatever string you won't find in your input, then replace ##1##to 1 – Máté Juhász – 2019-02-04T21:27:23.183

0

Not sure if VS supports lookbehind, but if it does, you can use:

  • Find: (?<=\w{5}) zero-length assertion to make sure we have 5 word characters before current position
  • Replace: 1

Toto

Posted 2019-02-04T21:12:06.413

Reputation: 7 722