Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 225
Mike Avatar asked Oct 24 '13 20:10

Mike


People also ask

How do you replace regex in Notepad?

In all examples, use select Find and Replace (Ctrl + H) to replace all the matches with the desired string or (no string). And also ensure the 'Regular expression' radio button is set. Check that your word wrap is switched off so that you actually see the text get modified.

Can you use regex to replace string?

The Regex. Replace(String, String, MatchEvaluator, RegexOptions) method is useful for replacing a regular expression match in if any of the following conditions is true: The replacement string cannot readily be specified by a regular expression replacement pattern.


1 Answers

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

like image 159
Tim Pietzcker Avatar answered Oct 12 '22 06:10

Tim Pietzcker