Community
    • Login

    Add double quotes @ start and en of line + text in midle +more

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 4 Posters 3.5k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Gilles GirardG
      Gilles Girard
      last edited by

      After many hours searching I now have a basic understanding (limited to autohotkey experience).
      Seeking help to put the pieces together.
      1- need to add double quotes to the start and end of each lines
      2- I need to make this a single line with additional text after the first line (or [Source.Name] =)

      The following list :
      Request 1.xls
      request 2.xls
      request 3.xls

      would become
      “Request 1.xls” or [Source.Name] = “request 2.xls” or [Source.Name] = “request 3.xls”

      PeterJonesP astrosofistaA Lufi SaL 3 Replies Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Gilles Girard
        last edited by

        @Gilles-Girard

        Not sure what autohotkey experience has to do with a Notepad++ search-and-replace.

        Use regular expressions [*]. For more info, see the Notepad++ Search and Replace docs, specifically for regular expressions: https://npp-user-manual.org/docs/searching/#regular-expressions

        For a one-off, I would do a multi-step process. It could be combined, but it’s easier to explain this way.

        1. add quotes around every non-empty line:
          • FIND = (?-s)^.+$
            • find each non-empty line
          • REPLACE = "$0"
            • replace it with the same string ($0 is the “text that was matched” placeholder), but with quotes around it
          • SEARCH MODE = regular expression
        2. replace newlines with [Source.Name] =
          • FIND = \R+(?!\Z)
            • \R means newline; the + means one or more; the stuff in parentheses means “that isn’t followed by the end of the file”, so this won’t replace the final newline in your file.
          • REPLACE = \x20or [Source.Name] =\x20
            • the \x20 are an alternate way of writing the space character; it works in regex, and it makes sure you don’t miss the leading or trailing space when copy/pasting from the forum
          • MODE = regular expression

        If this isn’t sufficient for you, read and follow the advice below

        ----

        Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as plain text using the </> toolbar button or manual Markdown syntax. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get… Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.

        1 Reply Last reply Reply Quote 3
        • astrosofistaA
          astrosofista @Gilles Girard
          last edited by

          Hi @Gilles-Girard

          If you are not still convinced by @PeterJones’ excellent advice, you can always construct the required line by means of an AutoHotkey script, as follows:

          SampleText =
          (
          Request 1.xls
          request 2.xls
          request 3.xls
          )
          
          myText := " or [Source.Name] = "
          myOutput := ""
          
          for _, Lines in StrSplit(SampleText, "`n") {
          	if (A_Index < 3)
          		myOutput .= """" Lines """" myText
          	else
          		myOutput .= """" Lines """"
          }
          
          MsgBox, % myOutput
          ; "Request 1.xls" or [Source.Name] = "request 2.xls" or [Source.Name] = "request 3.xls"
          

          Maybe there are simpler or better approaches in AutoHotkey, but I think that the regex solution is straightforward.

          Have fun!

          1 Reply Last reply Reply Quote 2
          • Gilles GirardG
            Gilles Girard
            last edited by

            Thank you both for the great answers.
            @ astrosofista thanks for the suggestion, I forgot to mentioned my goal was to use native language of NPP.

            @ PeterJones, Thanks for the pointers the “(?-s)$” , “\x20” , “\R+(?!\Z)” , etc. would have been days of trial and error.

            I recorded the 2 macro and merged them manually in shortcuts.xml.

            Works like a charm.

                    </Macro>
                        <Macro name="add quotes and source name and merge as single line" Ctrl="no" Alt="no" Shift="no" Key="0">
                        <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                        <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?-s)^.+$" />
                        <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                        <Action type="3" message="1602" wParam="0" lParam="0" sParam='&quot;$0&quot;' />
                        <Action type="3" message="1702" wParam="0" lParam="512" sParam="" />
                        <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                        <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                        <Action type="3" message="1601" wParam="0" lParam="0" sParam="\R+(?!\Z)" />
                        <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                        <Action type="3" message="1602" wParam="0" lParam="0" sParam="\x20or [Source.Name] =\x20" />
                        <Action type="3" message="1702" wParam="0" lParam="512" sParam="" />
                        <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                    </Macro>
            
            1 Reply Last reply Reply Quote 4
            • Lufi SaL
              Lufi Sa @Gilles Girard
              last edited by

              @Gilles-Girard said in Add double quotes @ start and en of line + text in midle +more:

              After many hours searching I now have a basic understanding (limited to autohotkey experience).
              Seeking help to put the pieces together.
              1- need to add double quotes to the start and end of each lines
              2- I need to make this a single line with additional text after the first line (or [Source.Name] =)
              The following list :
              Request 1.xls
              request 2.xls
              request 3.xls
              would become
              “Request 1.xls” or [Source.Name] = “request 2.xls” or [Source.Name] = “request 3.xls”

              Screenshot 2023-05-06 203733.png
              This script first initializes a variable fileList with the names of the files. It then loops through the list and adds double quotes to the start and end of each file name using the Chr() function to insert the ASCII code for a double quote (34).

              The script also creates a variable output to store the final output string. The first file name is added to the output variable without the “or [Source.Name] =” prefix, while the rest of the file names are appended to output with the prefix.

              Finally, the MsgBox command displays the final output string as a message box.

              You can customize the fileList variable to include your own list of files. Just replace the names in the square brackets with your own file names.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors