Replace text with text and new line

2. Help
KrashDummy
2013-02-21
2013-04-02
  • KrashDummy
    KrashDummy
    2013-02-21

    Hi.

    How can I Use the search and replace dialog box to replace some text with text AND newline? Have tested the most obvious ways - "\r\n" in replace field does not making the replacement text containing a new line, instead it just put the string "\r\n" :(

    A real world example - I have a large XML file (please do not suggest an extension to clean up xml code as this is not part of the solution) that have no line feeds at all.

    Therefore I want to replace all occurrences of "><" with ">{newline}<". How is that done, if possible at all?

    Thanks

     
  • Fool4UAnyway
    Fool4UAnyway
    2013-02-21

    You can use Regular Expression Mode instead of Extended Mode.
    Extended Mode does search for "extended characters" but replaces just the absolute characters you enter, i.e. it replaces literally what you enter.

    I found an annoyance in Notepad 6.3++ Regular Expression Mode.
    I wanted to check if all commas were followed by a space character.

    Find:
    ,[^ ]

    I assumed, like it always did, that this would not match a comma at the end of a line, that is, immediately followed by a newline character.
    However, this currently also matches a comma at the end of a line.

    I thought, perhaps this is because somewhere a dot for "any character" is set to match newline characters as well, although I never intentionally would have done that.

    So I tried to search the other way around, by looking for a comma with just any character following it.

    Find:
    ,.

    Strangely enough, this did NOT match a comma at the end of a line.
    This IS what I would expect and therefor, to me, the behavior for including and
    excluding newline characters from matches is inconsistent.

    To me, it seems clear that if ,. does NOT match a comma at the end of the line (which I expect), also ,[^ ] would NOT match a comma at the end of the line (that is, including the newline character).

    So, to be consistent, if a dot does not match a newline character, then these should also be excluded from any not-any-of-these-characters group.

     
  • KrashDummy
    KrashDummy
    2013-02-22

    Hi.

    It turned out using TextFix Search/replace dialog box (Ctrl+R) solved it for me.

    I don't think it's possible to use the default search/replace (Ctrl+H) dialog box to replace with new line character.
    I've tried but didn't managed to get it to work. If ". matches newline" is checked, then npp just simply add the "." character instead of replace with newline.

     
  • @KrashDummy:
    With the standard replace dialog, in "Regular expression" mode, you can replace "><" with ">\r\n<", and it works as expected.

    @Fool4UAnyway:
    The "anything but space", written as "[^ ]", works as intended. The fact that dot matches newlines or not has nothing to do with what characters in brackets match. Of course this is different from what a "grep" would find, since standard "grep" is line based and does not put the end of line character in the search space, and thus a character at the end of a line has no character following it.

    If you want to search for a comma not followed by some space character (including tab and end of lines) you can search for ",\S", or the more verbose ",[^[:space:]]", or specify all characters you consider spaces ",[^ \t\r\n]".

     
    • Fool4UAnyway
      Fool4UAnyway
      2013-04-02

      If you want to search for a comma not followed by some space character (including tab and end of lines) you can search for ",\S", or the more verbose ",[^[:space:]]", or specify all characters you consider spaces ",[^ \t\r\n]".

      I find this very annoying. Is there any way to have it work as simple as one might expect it to? It's hard work remembering to put all those other undesired characters in the list of characters to be excluded every time.

       
  • Joris Debonnet
    Joris Debonnet
    2013-03-11

    It is currently not possible to insert newlines with the 'Extended' Search/Replace feature in Notepad++: it's a bug that has been confirmed in 6.3 and will be fixed in the next version.

    But you can indeed simply use the Regular Expression option for it, even if you're not using any actual regular expressions.

     
    Last edit: Joris Debonnet 2013-03-11