Community
    • Login

    Help with extended search/replace

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 3 Posters 2.7k 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.
    • Matteo ColomboM
      Matteo Colombo
      last edited by

      Hi, suppose that I have this file:

      dada test1
      daeq test2
      dawf test3
      fawq test4
      aafa test5
      faag test6

      I would get this with replace:

      dada test1
      :example1
      daeq test2
      :example2
      dawf test3
      :example3
      fawq test4
      :example4
      aafa test5
      :example5
      faag test6
      :example6

      How can i had :example(number) between all lines?
      ThankYou,
      Matteo.

      Claudia FrankC 1 Reply Last reply Reply Quote 0
      • Claudia FrankC
        Claudia Frank @Matteo Colombo
        last edited by

        @Matteo-Colombo

        3 steps

        First step:
        place the cursor onto the first line first position (or press ctrl+home)
        press alt+c (the column mode editor opens)
        select Number to Insert
        Initial number = 1
        Increase by = 1
        Repeat = 1
        press OK

        Should result in

        1dada test1
        2daeq test2
        3dawf test3
        4fawq test4
        5aafa test5
        6faag test6

        Step2
        press alt+c again (cursor should still be at line 1 position 1)
        select text to insert and type :example

        Should result in

        :example1dada test1
        :example2daeq test2
        :example3dawf test3
        :example4fawq test4
        :example5aafa test5
        :example6faag test6

        Step3 (cursor should still be at line 1 position 1

        press ctrl+h
        uncheck wrap around
        check regular expression
        find what should contain

        (:example\d+)(.+)
        

        replace with

        \2\r\n\1
        

        press replace all

        should result in

        dada test1
        :example1
        daeq test2
        :example2
        dawf test3
        :example3
        fawq test4
        :example4
        aafa test5
        :example5
        faag test6
        :example6

        Regular expression for find what: (:example\d+)(.+)

        () means we do have two capture groups \1 and \2
        \1 contains :example followed by one or more digits
        and
        \2 contains the rest including end of line chars

        In replace with we reorganize the string as \2\r\n\1 means
        print the second matching group=\2
        add an end of line char=\r\n
        print the first matching group = \1

        Sounds more complicated than actually is.

        Cheers
        Claudia

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

          Hello, @matteo-colombo and @claudia-frank,

          Hi, Claudia ! Of course, your solution works fine, but you certainly noticed, that any number, ending each line, wanted by Matteo, is present twice, on two consecutive lines, didn’t you ? Just a nice case for regexes, only !!

          So,… what about this simple regex S/R :

          SEARCH (?-s)^.+?(\d+\R)

          REPLACE $0:example\1

          Preferably :

          • Move back to the very beginning of your file

          • In the Replace dialog, as Claudia said, uncheck the Wrap around option et check the Regular expression option

          • Fill up the search and replace zones

          • Click on the Replace All button

          Et voilà !

          Notes :

          • As usual, the (?-s) modifier implies that any dot special character which match a single standard character, only

          • The ending part (\d+\R) represents any number, whatever the number of digits, followed by the End of Line characters of the current line. As this part is surrounded by parentheses, it is stored as group 1

          • The middle part ^.+? stands for any non-null text, from beginning of line till the last character, before the ending number

          • In replacement, we rewrite, first, the entire searched line ( $0 ), followed by the string :example, followed itself, by the group 1 ( \1 ) that is to say the ending number, followed by the End of Line characters of the current line !

          Best Regards,

          guy038

          Claudia FrankC 1 Reply Last reply Reply Quote 1
          • Claudia FrankC
            Claudia Frank @guy038
            last edited by Claudia Frank

            @guy038

            Hi Guy,

            to be honest, I didn’t think about it but you are right, if this,
            I mean that the number at the end of the line is the number
            which should be used in the “example” line,
            is the case, then your solution is much nicer than mine :-)

            Cheers
            Claudia

            1 Reply Last reply Reply Quote 0
            • Matteo ColomboM
              Matteo Colombo
              last edited by

              @Claudia-Frank
              @guy038

              Thank you so much I wanted this a lot!

              Cheers,
              Matteo.

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