Regex: Match/Delete everything on a line except parenthesis (keep the parentheses and delete the rest of the...












0















I have this kind of line:



Word (mother) word 33 (453) word word 444 (4) word



The result should be: (mother) (453) (4)



I want to keep the parentheses and delete the rest of the words on the line. I try this regex, but not too good :(



Search: ([^!(]*?)|(|)



Replace by: 1










share|improve this question

























  • Please post the result you want to achieve for the example above.

    – harrymc
    Feb 9 at 11:16
















0















I have this kind of line:



Word (mother) word 33 (453) word word 444 (4) word



The result should be: (mother) (453) (4)



I want to keep the parentheses and delete the rest of the words on the line. I try this regex, but not too good :(



Search: ([^!(]*?)|(|)



Replace by: 1










share|improve this question

























  • Please post the result you want to achieve for the example above.

    – harrymc
    Feb 9 at 11:16














0












0








0








I have this kind of line:



Word (mother) word 33 (453) word word 444 (4) word



The result should be: (mother) (453) (4)



I want to keep the parentheses and delete the rest of the words on the line. I try this regex, but not too good :(



Search: ([^!(]*?)|(|)



Replace by: 1










share|improve this question
















I have this kind of line:



Word (mother) word 33 (453) word word 444 (4) word



The result should be: (mother) (453) (4)



I want to keep the parentheses and delete the rest of the words on the line. I try this regex, but not too good :(



Search: ([^!(]*?)|(|)



Replace by: 1







windows-10 notepad++ regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 9 at 11:19







Just Me

















asked Feb 9 at 11:12









Just MeJust Me

1819




1819













  • Please post the result you want to achieve for the example above.

    – harrymc
    Feb 9 at 11:16



















  • Please post the result you want to achieve for the example above.

    – harrymc
    Feb 9 at 11:16

















Please post the result you want to achieve for the example above.

– harrymc
Feb 9 at 11:16





Please post the result you want to achieve for the example above.

– harrymc
Feb 9 at 11:16










3 Answers
3






active

oldest

votes


















2
















  • Ctrl+H

  • Find what: (?:^|G)(?:h*w+h*)+((w+)h*)|(?:h*w+)*$

  • Replace with: $1

  • check Wrap around

  • check Regular expression

  • Replace all


Explanation:



  (?:^|G)      # non capture group, beginning of line OR restart from last match position
(?: # non capture group
h* # 0 or more horizontal spaces
w+ # 1 or more word characters
h* # 0 or more horizontal spaces
)+ # end group, may appear 1 or more times
( # start group 1
(w+) # 1 or more word characters surounded by parenthesis
h* # 0 or more horizontal spaces
) # end group 1
| # OR
(?: # non capture group
h*w+ # 0 or more horizontal spaces, followed by 1 or more word characters
)* # group may appear 0 or more times
$ # end of line


Result for given example:



(mother) (453) (4) 




enter image description here






share|improve this answer































    1














    first replace



    edited:



    .*?((.*?))


    with



    1


    then, replace



    ^(.*)).*


    with



    1


    Final output



    (mother)(453)(4)





    share|improve this answer





















    • 1





      What's the meaning of the first quotation mark? Did you really try it?

      – Toto
      Feb 9 at 12:47











    • You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.

      – VSRawat
      Feb 9 at 13:03





















    0














    A simple regex to extract a word with its parenthesis is:



    (([^)]+))





    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1403824%2fregex-match-delete-everything-on-a-line-except-parenthesis-keep-the-parenthese%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2
















      • Ctrl+H

      • Find what: (?:^|G)(?:h*w+h*)+((w+)h*)|(?:h*w+)*$

      • Replace with: $1

      • check Wrap around

      • check Regular expression

      • Replace all


      Explanation:



        (?:^|G)      # non capture group, beginning of line OR restart from last match position
      (?: # non capture group
      h* # 0 or more horizontal spaces
      w+ # 1 or more word characters
      h* # 0 or more horizontal spaces
      )+ # end group, may appear 1 or more times
      ( # start group 1
      (w+) # 1 or more word characters surounded by parenthesis
      h* # 0 or more horizontal spaces
      ) # end group 1
      | # OR
      (?: # non capture group
      h*w+ # 0 or more horizontal spaces, followed by 1 or more word characters
      )* # group may appear 0 or more times
      $ # end of line


      Result for given example:



      (mother) (453) (4) 




      enter image description here






      share|improve this answer




























        2
















        • Ctrl+H

        • Find what: (?:^|G)(?:h*w+h*)+((w+)h*)|(?:h*w+)*$

        • Replace with: $1

        • check Wrap around

        • check Regular expression

        • Replace all


        Explanation:



          (?:^|G)      # non capture group, beginning of line OR restart from last match position
        (?: # non capture group
        h* # 0 or more horizontal spaces
        w+ # 1 or more word characters
        h* # 0 or more horizontal spaces
        )+ # end group, may appear 1 or more times
        ( # start group 1
        (w+) # 1 or more word characters surounded by parenthesis
        h* # 0 or more horizontal spaces
        ) # end group 1
        | # OR
        (?: # non capture group
        h*w+ # 0 or more horizontal spaces, followed by 1 or more word characters
        )* # group may appear 0 or more times
        $ # end of line


        Result for given example:



        (mother) (453) (4) 




        enter image description here






        share|improve this answer


























          2












          2








          2









          • Ctrl+H

          • Find what: (?:^|G)(?:h*w+h*)+((w+)h*)|(?:h*w+)*$

          • Replace with: $1

          • check Wrap around

          • check Regular expression

          • Replace all


          Explanation:



            (?:^|G)      # non capture group, beginning of line OR restart from last match position
          (?: # non capture group
          h* # 0 or more horizontal spaces
          w+ # 1 or more word characters
          h* # 0 or more horizontal spaces
          )+ # end group, may appear 1 or more times
          ( # start group 1
          (w+) # 1 or more word characters surounded by parenthesis
          h* # 0 or more horizontal spaces
          ) # end group 1
          | # OR
          (?: # non capture group
          h*w+ # 0 or more horizontal spaces, followed by 1 or more word characters
          )* # group may appear 0 or more times
          $ # end of line


          Result for given example:



          (mother) (453) (4) 




          enter image description here






          share|improve this answer















          • Ctrl+H

          • Find what: (?:^|G)(?:h*w+h*)+((w+)h*)|(?:h*w+)*$

          • Replace with: $1

          • check Wrap around

          • check Regular expression

          • Replace all


          Explanation:



            (?:^|G)      # non capture group, beginning of line OR restart from last match position
          (?: # non capture group
          h* # 0 or more horizontal spaces
          w+ # 1 or more word characters
          h* # 0 or more horizontal spaces
          )+ # end group, may appear 1 or more times
          ( # start group 1
          (w+) # 1 or more word characters surounded by parenthesis
          h* # 0 or more horizontal spaces
          ) # end group 1
          | # OR
          (?: # non capture group
          h*w+ # 0 or more horizontal spaces, followed by 1 or more word characters
          )* # group may appear 0 or more times
          $ # end of line


          Result for given example:



          (mother) (453) (4) 




          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 9 at 11:30









          TotoToto

          3,914101226




          3,914101226

























              1














              first replace



              edited:



              .*?((.*?))


              with



              1


              then, replace



              ^(.*)).*


              with



              1


              Final output



              (mother)(453)(4)





              share|improve this answer





















              • 1





                What's the meaning of the first quotation mark? Did you really try it?

                – Toto
                Feb 9 at 12:47











              • You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.

                – VSRawat
                Feb 9 at 13:03


















              1














              first replace



              edited:



              .*?((.*?))


              with



              1


              then, replace



              ^(.*)).*


              with



              1


              Final output



              (mother)(453)(4)





              share|improve this answer





















              • 1





                What's the meaning of the first quotation mark? Did you really try it?

                – Toto
                Feb 9 at 12:47











              • You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.

                – VSRawat
                Feb 9 at 13:03
















              1












              1








              1







              first replace



              edited:



              .*?((.*?))


              with



              1


              then, replace



              ^(.*)).*


              with



              1


              Final output



              (mother)(453)(4)





              share|improve this answer















              first replace



              edited:



              .*?((.*?))


              with



              1


              then, replace



              ^(.*)).*


              with



              1


              Final output



              (mother)(453)(4)






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 9 at 13:00

























              answered Feb 9 at 12:40









              VSRawatVSRawat

              15512




              15512








              • 1





                What's the meaning of the first quotation mark? Did you really try it?

                – Toto
                Feb 9 at 12:47











              • You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.

                – VSRawat
                Feb 9 at 13:03
















              • 1





                What's the meaning of the first quotation mark? Did you really try it?

                – Toto
                Feb 9 at 12:47











              • You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.

                – VSRawat
                Feb 9 at 13:03










              1




              1





              What's the meaning of the first quotation mark? Did you really try it?

              – Toto
              Feb 9 at 12:47





              What's the meaning of the first quotation mark? Did you really try it?

              – Toto
              Feb 9 at 12:47













              You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.

              – VSRawat
              Feb 9 at 13:03







              You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.

              – VSRawat
              Feb 9 at 13:03













              0














              A simple regex to extract a word with its parenthesis is:



              (([^)]+))





              share|improve this answer




























                0














                A simple regex to extract a word with its parenthesis is:



                (([^)]+))





                share|improve this answer


























                  0












                  0








                  0







                  A simple regex to extract a word with its parenthesis is:



                  (([^)]+))





                  share|improve this answer













                  A simple regex to extract a word with its parenthesis is:



                  (([^)]+))






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 9 at 12:20









                  harrymcharrymc

                  262k14271578




                  262k14271578






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1403824%2fregex-match-delete-everything-on-a-line-except-parenthesis-keep-the-parenthese%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                      Mangá

                       ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕