• Login
Community
  • Login

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

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
4 Posts 3 Posters 1.2k 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.
  • G
    Gabriel Mourão
    last edited by Jun 19, 2022, 9:27 PM

    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?

    A 1 Reply Last reply Jun 19, 2022, 9:54 PM Reply Quote 0
    • A
      Alan Kilborn @Gabriel Mourão
      last edited by Jun 19, 2022, 9:54 PM

      @gabriel-mourão

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

      G 1 Reply Last reply Jun 19, 2022, 10:25 PM Reply Quote 3
      • G
        Gabriel Mourão @Alan Kilborn
        last edited by Gabriel Mourão Jun 19, 2022, 10:27 PM Jun 19, 2022, 10:25 PM

        @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
        • G
          guy038
          last edited by guy038 Jun 20, 2022, 12:15 PM Jun 20, 2022, 12:05 PM

          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
          2 out of 4
          • First post
            2/4
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors