Using Regular Expressions with \n on Windows

2014-08-12
2014-08-12
  • Walt Farrell
    Walt Farrell
    2014-08-12

    My apologies if this is documented elsewhere, but I couldn't find it, and after finally figuring it out I thought I'd mention it here in case it helps someone else.

    I have found that NP++ works differently on Windows than other Windows programs I have that support regular expressions. In order to perform RegEx searches that work across lines, which just uses \n in the other programs, I have to use \r\n in NP++. Once I convert all my \n into \r\n in my regular expressions, the searches work just as I expect.

    For example, suppose my file contains these two lines:
    abcdef
    ghijkl

    A RegEx search (using ctrl-F or ctrl-H) for f\ng will not find anything. But searching for f\r\ng works just fine. This is surprising, and a bit annoying, but it's something I can live with, and better than not being able to do the searches at all :)

     
  • dail8859
    dail8859
    2014-08-12

    This is because Notepad++ does not convert line endings. It is able to handle Windows and Unix line endings. To search for any type of line endings use \R

     
  • Thomas
    Thomas
    2014-08-12

    Hi,

    You should distinct two different scenarios:

    1. The operating system dependent way how "end of line" characters are represented.
    2. How regular expressions can match such special characters

    In Windows "end of line" are usually \r\n in Linux this usually is \n only. If you build your regular expressions operating system dependent of course you must edit them when switching from one operating system to another.

    By default Notepad++ use the Windows representation (\r\n) for text files. If you want to stay at your \n "end of line" representation in your regular expressions you can consider to convert the files. This can easily be done from the menu "Edit" -> "EOL conversion" -> "Convert to UNIX format". You should convert the "end of line" after you are done with your regular expression so all standard Windows program can handle your edited text files.

    Within Notepad++ you can use the term \R to match any "end of line" (as described at http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html#boost_regex.syntax.perl_syntax.matching_line_endings). This of course will raise the need to change your regular expression again and only makes sense if the program you are using in the UNIX environment also supports this syntax.

    Regards,
    Thomas

     
  • Walt Farrell
    Walt Farrell
    2014-08-12

    Thanks for the info.

    Another question: how is a user of NP++ expected to know that? In other words, where is the documentation that tells us to look there? That's something else I haven't been able to find.