16

Just to preface, I have read many of the questions regarding this, but I couldn't find an answer (at least in my digging). Feel free to point it out if I did miss it!

I know how to use a regex, and in fact I am finding the text I am searching for. However, when I try to do as other questions suggested "\1MyAppendedTextHere" and replace, it just erases the matched pattern and adds what follows the \1. (Previous questions I looked up stated that the "\1" was how notepad++ did this). Has this changed? Am I doing something wrong?

Here is what it looks like:

find: name[A-Z_0-9]+
replace: \1_SUFFIX

Any help is appreciated!

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
Mike
  • 585
  • 1
  • 10
  • 24
  • 2
    FWIW, I noticed in v6.6.8, the captures are represented by $0, $1, etc. The $ notation is also mentioned by other comments below. On the other hand, the backslash notation mentioned below did not work for me except to escape parentheses for output. – bvj Sep 01 '14 at 04:04

1 Answers1

25

\1 references the contents of the first capturing group, which means the first set of parentheses in your search regex. There isn't one in your regex, so \1 has nothing to refer to.

Use \0 if you want to reference the entire match, or add parentheses around the relevant part of the regex.

find: name[A-Z_0-9]+
replace: \0_SUFFIX

will change nameABC into nameABC_SUFFIX.

Using capturing groups, you can do things like

find: name([A-Z_0-9]+)     
replace: \1_SUFFIX

which will replace nameABC with ABC_SUFFIX.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 2
    In addition, since Notepad++ uses the PCRE regular expression engine, you should avoid the use of `\1` and get used to `$1` (see http://stackoverflow.com/q/1068840/2140859) – psxls Oct 24 '13 at 20:20
  • @psxls: Are you sure about this? Perl isn't PCRE ("Perl-compatible regex", a different engine). – Tim Pietzcker Oct 24 '13 at 20:24
  • @Tim, thanks for you comment because it made me think and I believe I might be wrong. I'm aware of the difference between PCRE and Perl regex engine, but I naively assumed that since PCRE imitates the syntax and semantics of Perl, same warning apply. I checked [PCRE's man page](http://www.pcre.org/pcre.txt) and didn't found any reference to the issue, while [perlre](http://perldoc.perl.org/perlre.html#Warning-on-\1-Instead-of-$1)'s explanation giving 2 examples of why you should avoid `\1` doesn't apply in Notepad++ case. – psxls Oct 24 '13 at 22:20
  • On the other hand, PHP which uses PCRE [states](http://php.net/manual/en/function.preg-replace.php) that `$1` form is preferred: *replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred on*. It make sense I guess since the [/e](http://php.net/manual/en/reference.pcre.pattern.modifiers.php) modifier could cause the same trouble as in Perl. – psxls Oct 24 '13 at 22:23
  • Thanks for this. I used this to append a prefix. I was searching for dates in a file with - and I wanted to make this into a CSV to import into certain sites. My search in Notepad++ was: Find: -([0-9][0-9][0-9][0-9]) Repace: ,$1 Thanks this saved me a ton of time! – Brian Jan 07 '20 at 23:44