Mike
Mike

Reputation: 615

Notepad++ Regex replace - \1 doesn't work?

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!

Upvotes: 16

Views: 16737

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336138

\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.

Upvotes: 25

Related Questions