Add text to beginning of line changes the content



  • I am trying to paste in a sql query into notepad++ and it is taking out part of the line.
    I have done this previously and it worked so i am doing it correctly.

    So what i am doing is running a sql querry to get a list of numbers for updates to delete.

    I paste those into notepad++ and Use ^ to append to the beginning and $ for the end. (using regular expression also).

    So at the beginning i am trying to add this:
    sqlcmd -Snp:\.\pipe\MICROSOFT##WID\tsql\query -Q "EXEC SUSDB.dbo.spDeleteUpdate

    And what i get is this:
    sqlcmd -Snp:.pipeMICROSOFT##WID sqlquery -Q "EXEC SUSDB.dbo.spDeleteUpdate

    it takes out the \t and the following \
    so it takes \tsql\query and becomes sqlquery

    I have tried it with the latest release (64 and 32 bit) and also 7.42 (32 bit)

    FYI If i paste it one line at a time it works, but with hundreds of lines, well you know!

    Any help would be appreciated. thanks Tim



  • @Tim-Perreault

    Since you are using regular expression search mode you have to “escape” the meanings of the special characters regex uses when you want them literally. Thus for literal \ use \\, for literal . use \., etc.

    I don’t have quick access to the complete list for Notepad++'s regex engine, but this link should cover most if not all of the characters that need this escaping.

    You are working in the Replace-with zone and its requirements for what needs escaping likely vary somewhat from the escaping requirements of the Find-what zone, but this should put you on the right track to success. :-)



  • Thanks that worked.



  • @Tim-Perreault

    If this is something that you do often, you can record a macro to help:

    So, record this action as a macro and name it “Escape Selection”:

    Find what zone: ([][\.^$*+?(){}\\|-])|(\R)
    Replace with zone: \\(?1\1:R)
    Search mode radio-button: Regular expression
    In selection checkbox: ticked

    To use it, you would highlight the text (which can even span lines) you need to be verbatim (but expressed as a regex!), run the macro, it will change the text as needed, you copy the text to the clipboard and Undo the change. Then (for your described usage) when you move to the Replace window’s Replace-with zone, you paste in the clipboard contents. It sounds lengthier when described than it actually is to do it.

    Thus in your example, you would highlight this text and run the macro:

    sqlcmd -Snp:\.\pipe\MICROSOFT##WID\tsql\query -Q "EXEC SUSDB.dbo.spDeleteUpdate
    

    It will change the text to this and leave it selected for copying (well, maybe…on the leave it selected part):

    sqlcmd \-Snp:\\\.\\pipe\\MICROSOFT##WID\\tsql\\query \-Q "EXEC SUSDB\.dbo\.spDeleteUpdate
    

    Do an Undo and you are back where you started except you have an escaped string in the clipboard, that you can then do whatever you need with.

    Hopefully I hit all the characters that need escaping, but if not this is tweakable. Another improvement might be to add the copy action into the macro at the end and call the macro “Escape Selection and Copy”.

    [Note that this does require you to have the source text in a Notepad++ editing tab and not merely in a Find-what or Replace-with zone, but this is often the case…]



  • @Scott-Sumner said:

    leave it selected for copying (well, maybe…on the leave it selected part)

    What I meant by the above is that I noticed that if a character that needs to be escaped occurs as the last character in the starting selection, after the macro runs the 2 characters generated for it are omitted from the new selection. So the selection would have to be lengthened two by 2 characters before copying would be effective. :(

    This isn’t a downfall of this technique but rather an apparent bug in Notepad++'s replace-in-selection logic…see https://github.com/notepad-plus-plus/notepad-plus-plus/issues/2995



  • Slight tweak because \R only works in the Find-what zone, so if the intended destination for the escaped text is to be the Replace-with zone, the earlier wouldn’t work.

    Revised Replace-with zone: \\(?1\1:r\\n)

    Note that the revision is Windows line-ending (CRLF) specific, but this shouldn’t be much of an issue.


Log in to reply