Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

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

    Help wanted · · · – – – · · ·
    replace
    3
    7
    94
    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 Philip
      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...
      
      datatraveller1 1 Reply Last reply Reply Quote 0
      • datatraveller1
        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

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

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • Anil Philip
            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.

            datatraveller1 1 Reply Last reply Reply Quote 0
            • datatraveller1
              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]

              datatraveller1 1 Reply Last reply Reply Quote 0
              • datatraveller1
                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
                • guy038
                  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
                  Copyright © 2014 NodeBB Forums | Contributors