Bohemian
Bohemian

Reputation: 425033

How to delimit a regex group number in notepad++ replacement?

I want to specify a captured group then a literal digit in a replacement term, but the literal digit is being interpreted as part of the group number.

Given this (contrived) example:

Input text: A5
Find: (.)(.)
Replace: $16
Expected result: A6
Actual result: <blank>

Experimentation suggests that $16 is being interpreted as "group 16".

I tried using $1\6 to make the 6 literal, which gave me group 1, but a blank for the \6 - ie the result was just A. $1\\6 gave me A\6.

The general question is, "how do I specify group 1 then a literal number"?

Upvotes: 5

Views: 512

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

Notepad S&R regex is powered by Boost regex library.

The unambiguous $n backreference is achieved with braces ({}) around the ID, so, you can use ${1}6 as a replacement here.

Notepad++ also supports BRE style backreferences starting with \ (\1, \2 etc. *up to 9). So, when you use \16 in the replacement pattern, the engine will only parse it as Backreference 1 + a literal symbol 6. You may check it by replacing (.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.) with \11 in 1234567890A. Instead of the A (the 11th group) you will get 11 as a result. $11 replacement would result in A.

Notepad++ help mentions these notations but it lacks details:

$n, ${n}, \n
Returns what matched the subexpression numbered n. Negative indices are not alowed.

Upvotes: 6

Related Questions