Community
    • Login

    Find a string using Regular Expression, Store the result and use it in another search as replace with text

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    search & replaceregexfindtext macrosearch & replace
    6 Posts 2 Posters 991 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.
    • B Sethi24B
      B Sethi24
      last edited by

      Looking to Find string using RegularExpression like
      FindWhat: - *name.lqr" and
      if a string found of this RegEx pattern then store the value of found string in a variable or somewhere else
      so that this stored value can be used in next FindReplace as a ReplaceWith value.

      Example: -
      First Find with Regular Expression: -
      *name.lqr"

      Suppose, by this we found a string -> name_exam_test_ratio_lqr"
      Then need to temporarily save this string either in a variable or somewhere else for another Find&Replace.
      

      Now, need to do Find&Replace with Regular Expression only for another expression in Find like: -
      Find What: - .*mov"
      Replace With: - <The String found in “First Find” i.e. name_exam_test_ratio_lqr"

      Please advice: -
      1) Any way to achieve this in Notepad++ via Find&Replace &
      2) Can we have this in Macro also?

      Thanks!

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @B Sethi24
        last edited by

        @B-Sethi24

        1. Nope
        2. Nope

        Great task for a scripting language, though!

        1 Reply Last reply Reply Quote 1
        • B Sethi24B
          B Sethi24
          last edited by

          Thanks Alen for speedy response … Could you please advice which script is supported in Notepad++ and how to achive this please…Thanks!

          Alan KilbornA 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @B Sethi24
            last edited by

            @B-Sethi24 said in Find a string using Regular Expression, Store the result and use it in another search as replace with text:

            Could you please advice

            Well, I’d go with Pythonscript (it’s a Notepad++ plugin).
            Let me throw together a quick demo; if needed you could then build upon it yourself.

            Alan KilbornA 1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @Alan Kilborn
              last edited by

              Here’s a real quick demo:

              # -*- coding: utf-8 -*-
              
              from Npp import editor, notepad
              
              def demo():
                  user_input = notepad.prompt('First expr:', '', 'foo')
                  matches = []
                  editor.research(user_input, lambda m: matches.append(m.span(0)), 0, 0, editor.getLength(), 1)
                  if len(matches):
                      first_match_text = editor.getTextRange(matches[0][0], matches[0][1])
                      user_input = notepad.prompt('Second expr:', '', 'bar')
                      editor.rereplace(user_input, first_match_text, 0, 0, editor.getLength(), 1)
              
              demo()
              
              1 Reply Last reply Reply Quote 3
              • Alan KilbornA
                Alan Kilborn
                last edited by Alan Kilborn

                Additionally, here are some basic instructions for installing Pythonscript and getting a script to run:

                • https://community.notepad-plus-plus.org/post/54655

                And here’s some stuff about binding the execution of a script to a keycombo:

                • https://community.notepad-plus-plus.org/post/55131
                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors