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 1.4k 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 Offline
      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 Online
        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 Offline
          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 Online
            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 Online
              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 Online
                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

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors