4

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"?

Bohemian
  • 412,405
  • 93
  • 575
  • 722

1 Answers1

5

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Yes, because these are BRE style backreference and there are only 1 to 9 such backreferences supported. – Wiktor Stribiżew Sep 02 '16 at 00:06
  • The opening paragraph of the help page has a link (see *"on the implementer's website"*) to the full details of the regular expression language. I cannot see a link there to the replacement language, but it can be found at http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html . Links to both documents can be found at http://stackoverflow.com/a/16104946/546871 – AdrianHHH Sep 02 '16 at 08:37