Notepad++ Regex Backreference syntax in Search/Replace - \1 or $1

32,967

Notepad++'s earlier versions (v5.9.8 and prior) only supported standard POSIX Regular Expressions. However, full PCRE (Perl Compatible Regular Expression) Search/Replace support was added in version 6.0:

New features and enhancement in Notepad++ 6.0:

  • PCRE (Perl Compatible Regular Expressions) is supported.

This means that if you're using Notepad++ v6.0 or any newer version (e.g v6.1.5), you can use the PCRE syntax, and use $1 instead of \1 for backreference, but it won't be compatible with earlier versions of Notepad++ (prior to version 6.0). Other than that, they're similar.

For more info regarding the differences between the backreference syntax and the reasons behind the new syntax support, see Backreferences syntax in replacement strings (why dollar sign?).

A useful tutorial on how to use regular expressions in Notepad++ can be found here.

Share:
32,967
amiregelz
Author by

amiregelz

Updated on August 01, 2020

Comments

  • amiregelz
    amiregelz almost 4 years

    I have tried to use the Notepad++ Search/Replace with a Regular Expression to replace specific words with shorter versions of those words.


    I used the following regex to match every word that ends with er (but not er as a word) - and replace the matching words with the same words minus the ending r, using a backreference:

    Find what: ([a-zA-z]+e)r

    Replace with: $1

    But it doesn't replace the matching words, even though it finds them.

    However, if I change the backreference syntax to this:

    Replace with: \1

    Everything works fine.


    Why doesn't the $1 backreference work?

    What is the difference between the two forms of the backrefernce - \1 and $1?

  • Brian Lacy
    Brian Lacy about 10 years
    @amiregelz I've been using (and loving) Notepad++ for many years, but one of the biggest frustrations I've had with it has been its poor Regex support. I can't believe I've been in the dark on these enhancements for TWO YEARS. Standard back-references, zero-width assertions -- it's beautiful! Thank you for opening my eyes!
  • JGFMK
    JGFMK almost 4 years
    - yup doing things like searching for consecutive lines that start with same word etc. It rocks ;-) (searchtext)[^\n]*\n\1