Community
    • Login

    Find/Replace - keeping a part of the find, remove everything else

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 1.1k 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.
    • Gabriel MourãoG
      Gabriel Mourão
      last edited by

      To sum it up take this example im trying to reproduce:

      This is the contents of document.txt:

      ![\vec{Q}=m \cdot \vec{\Delta v}](vec{Q}=m_!cdot_!vec{!Delta_v})
      

      For the documents i have created, there are hundreds of patterns like these, and i want to dwindle them down to:

      $\vec{Q}=m \cdot \vec{\Delta v}$
      

      To do that, i would have to locate all structures just like this one inside the document, which i did with:

      \!\[+[^]]+[A-Za-z{}=\/!_-\s]]\(+[^)]+[A-Za-z{}=\/()!_-\s])
      

      The problem right now is that i have no idea how to a keep a part of the find, or if its even possible. Until now all the replaces i did were only literal, replacing a find with a select string. Is there a way to separate a part of the found characters as a variable and keep them on replacement?

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @Gabriel Mourão
        last edited by

        @gabriel-mourão

        You want “capture groups” and substitutions using them.
        See the user manual HERE.

        Gabriel MourãoG 1 Reply Last reply Reply Quote 3
        • Gabriel MourãoG
          Gabriel Mourão @Alan Kilborn
          last edited by Gabriel Mourão

          @alan-kilborn I took a look at it, added a few tweaks (by that i mean just two parantheses) and now it works perfectly. Thanks alot.
          For those who might be curious to how the solution looks like:

          \!\[+([^]]+[A-Za-z{}=\/!_-\s])]\(+[^)]+[A-Za-z{}=\/()!_-\s]
          

          edit: and for the replace:

          \$$1\$
          
          1 Reply Last reply Reply Quote 3
          • guy038G
            guy038
            last edited by guy038

            Hello, @gabriel-mourão, @alan-kilborn and All,

            I’m sorry, Gabriel, but your final search regex seems invalid -:((

            May be it’s some typos, introduced by the forum syntax, but one correct syntax is :

            !\\[([^]]+[A-Za-z{}=\\/!_-\s])\\]\([^)]+[A-Za-z{}=\\/()!_-\s]
            

            Using the free-spacing mode, (?x), this syntax can be split as below :

            (?x)
            
            !\\[
            (
            [^]]+
            [A-Za-z{}=\\/!_-\s]
            )
            \\]
            
            \(
            [^)]+
            [A-Za-z{}=\\/)!_-\s]
            

            Note that :

            • On one hand :

              • The part [^]]+ matches the chars range ![\vec{Q}=m \cdot \vec{\Delta v}

              • The part [A-Za-z{}=\\/!_-\s] matches the single char ]

            • On the other hand :

              • The part [^)]+ matches the chars range (vec{Q}=m_!cdot_!vec{!Delta_v}

              • The part [A-Za-z{}=\\/)!_-\s] matches the single char ), at the end

            I suppose that it’s not exactly what you want !


            Your need could rather be expressed as :

            • First search for an exclamation mark and an opening square bracket ![

            • Then, search for the smallest range of chars…

            • Till an ending square bracket ]

            • Now, search for an opening parenthese (

            • Then, search for the smallest range of chars…

            • Till an ending parenthese )

            This leads to this simple regex S/R :

            SEARCH (?-s)!\\[(.+?)\\]\(.+?\)

            REPLACE \$$1\$

            Best Regards,

            guy038

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