Community
    • Login

    Find-Replace. Can NPP "remember" the character and transfer it to the output?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    replace
    7 Posts 3 Posters 332 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.
    • Anil PhilipA
      Anil Philip
      last edited by

      Fellow Notepad++ Users,

      Could you please help me with the following search-and-replace problem I am having?

      I understand in the Find-Replace menu that one can use ‘.’
      to represent any character. However can NPP “remember” the character and transfer it to the output?

      if I find [3] then replace with consec(3)

      Here is the data I currently have (“before” data):

      [3],[5],[4],[4],[3],[4],[3],[5],[2],[5],[2],[3],[2],[1],[5],[3],[5],[5],[2],[3],[1],[1],[3],[4],[5],[1],[3],[4],[3],[5],[2],[5],[4],[4],[5],[3],[1],[5],[1],[1],[4],[1],[5],[4],[3],[2],[3],[1],[2],[5]
      

      Here is how I would like that data to look (“after” data):

      System.out.println(dataStream.consec(3));
      System.out.println(dataStream.consec(5));
      System.out.println(dataStream.consec(4));
      System.out.println(dataStream.consec(4));
      System.out.println(dataStream.consec(3));
      System.out.println(dataStream.consec(4));
      System.out.println(dataStream.consec(3));
      System.out.println(dataStream.consec(5));
      System.out.println(dataStream.consec(2));
      System.out.println(dataStream.consec(5));
      ....and so on...
      
      datatraveller1D 1 Reply Last reply Reply Quote 0
      • datatraveller1D
        datatraveller1 @Anil Philip
        last edited by datatraveller1

        @Anil-Philip said in Find-Replace. Can NPP “remember” the character and transfer it to the output?:

        [1],[5],[3],[5],[5],[2],[3],[1],[1],[3],[4],[5],[1],[3],[4],[3],[5],[2],[5],[4],[4],[5],[3],[1],[5],[1],[1],[4],[1],[5],[4],[3],[2],[3],[1],[2],[5]

        This works for me:
        Ctrl+H (Replace):
        Find What: \\[([0-9])\\],?
        Replace With: System.out.println\(dataStream.consec\(\1\)\);\n
        Regular expression: checked
        Wrap around: checked
        Replace All

        \1 remembers the data of the first pair of parentheses

        datatraveller1D Anil PhilipA 2 Replies Last reply Reply Quote 3
        • datatraveller1D
          datatraveller1 @datatraveller1
          last edited by datatraveller1

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • Anil PhilipA
            Anil Philip @datatraveller1
            last edited by

            @datatraveller1 Thank you! Can you please clarify what the ? does? Over here it says: Matches the previous element zero or one time.

            datatraveller1D 1 Reply Last reply Reply Quote 0
            • datatraveller1D
              datatraveller1 @Anil Philip
              last edited by

              @Anil-Philip Yes, it means that the number in square brackets may or may not be followed by a comma, the last one is not:
              … [1],[2],[5]

              datatraveller1D 1 Reply Last reply Reply Quote 0
              • datatraveller1D
                datatraveller1 @datatraveller1
                last edited by datatraveller1

                Hi @guy038 and all: Is it possible to just search for the square brackets with numbers in them (without the comma part) and create the result string from that? While my solution works, I have the impression there might be a more elegant solution.

                1 Reply Last reply Reply Quote 0
                • guy038G
                  guy038
                  last edited by guy038

                  Hello, @datatraveller1 and All,

                  No, I think that your regex S/R is just the right one and cannot be improved significantly ;-))

                  Perhaps it’s good to recall that, at the end of your replacement string, you must insert \r\n if you use Windows files OR \n if you use Unix files


                  An other formulation would be to split out this S/R in two individual S/Rs :

                  • SEARCH [([0-9])],?

                  • REPLACE $1\)\);\r\n

                  Then :

                  • SEARCH (?-s)^(?=.)

                  • REPLACE System.out.println\(dataStream.consec\

                  But, no advantage for doing this as the first search regex is identical in the two cases !

                  Best Regards,

                  guy038

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