• Login
Community
  • Login

How can I find strings between "(" and ")" with any number of characters in between?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
10 Posts 4 Posters 15.6k 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.
  • M
    Munter Göök
    last edited by Mar 18, 2021, 10:33 AM

    I wish to enter a search string in the find box that finds any combination of (…) where … can be of any type of character and any number of characters.

    My intention is to remove all such ocurrences from a file.
    Ex: (gunshot), (grunt), (crashes) or whatever.

    A 1 Reply Last reply Mar 18, 2021, 11:39 AM Reply Quote 0
    • A
      Alan Kilborn @Munter Göök
      last edited by Mar 18, 2021, 11:39 AM

      @Munter-Göök

      You want to use regular expression search mode for your replace.

      A ( or a ) has special meaning, so if you want to match them literally, you need to use \( and \).

      To match any character, you use . to represent it.

      If you want the . to match line-ending characters, you specify (?s) somewhere before the . usage occurs. If you don’t want it to match line-ending characters, you specify (?-s).

      If you want to repeat a specification one or more times, you put a + after it. If you want to repeat it minimally, you put a +? after it.

      Putting it all together, you might try searching for (?-s)\(.+?)\) and replacing with nothing.

      1 Reply Last reply Reply Quote 3
      • M
        Munter Göök
        last edited by Mar 19, 2021, 9:00 AM

        Thanks Alan but (?-s)(.+?)) doesn’t work at all. It says “Invalid regular expression”. I will give you a part of the text in a file:

        00:02:21,850 --> 00:02:23,601
        (gunshot)

        • Howdy.
        • (man) Come on, Iad.

        00:06:16,293 --> 00:06:18,878
        I’m a pugiIist. (laughs)

        • Yeah, sure.
        • (John) WeII, excuse me, BiIIy.

        00:16:12,055 --> 00:16:14,056
        (♪ jig)

        Here I want to remove:
        (gunshot)
        (man)
        (laughs)
        (John)
        (♪ jig)

        Please try if you have the time. I attach a dump of the search. Hope it works…
        ss.png

        Thanks anyway!

        A 1 Reply Last reply Mar 19, 2021, 11:49 AM Reply Quote 0
        • A
          Alan Kilborn @Munter Göök
          last edited by Alan Kilborn Mar 19, 2021, 11:51 AM Mar 19, 2021, 11:49 AM

          @Munter-Göök

          Ha. Oops. An extra ) snuck its way in there.
          I’m surprised that you or others reading the post didn’t notice that.
          I mean, well, you can blindly apply what people give you, but if you look at it and try to figure out WHY it works (or doesn’t), perhaps some insight occurs. Probably also because I described what was going on with it, that might have helped with this.

          Try: (?-s)\(.+?\)

          Anyway, sorry for the misdirection.

          M 1 Reply Last reply Mar 23, 2021, 11:21 AM Reply Quote 1
          • G
            guy038
            last edited by Mar 19, 2021, 1:19 PM

            Hi, @alan-kilborn,

            You said :

            I’m surprised that you or others reading the post didn’t notice that.

            However, Alan, it’s not always easy to see, at once, when a regex is invalid ;-))


            Indeed, would you have seen, at first glance, that the regex, below, is invalid ?

            SEARCH (?-is)\\\\\((\((bcxy)\\a\2z\\\2))\)\\\\\d+\\\\\1\\\\

            whereas this similar regex is totally correct :

            SEARCH (?-is)\\\\\((\((bcxy)\\a\2z\\\2\))\)\\\\\d+\\\\\1\\\\

            and matches the text \\((bcxy\abcxyz\bcxy))\\12345\\(bcxy\abcxyz\bcxy)\\


            Hence, the advantage of using the free-spacing mode for complicated regex syntaxes !

            Indeed, this regex can now be expressed as :

            (?x-is) \\ \\ \( ( \( ( bcxy ) \\ a \2 z \\ \2 \) ) \) \\ \\ \d+ \\ \\ \1 \\ \\

            Note that I deleted the \ char, after the last \2 syntax, in the invalid regex !

            BR

            guy038

            A 1 Reply Last reply Mar 19, 2021, 1:23 PM Reply Quote 2
            • A
              Alan Kilborn @guy038
              last edited by Mar 19, 2021, 1:23 PM

              @guy038 said in How can I find strings between "(" and ")" with any number of characters in between?:

              it’s not always easy to see, at once, when a regex is invalid ;-))

              Agreed.

              would you have seen, at first glance, that the regex, below, is invalid ?

              SEARCH (?-is)\\\\\((\((bcxy)\\a\2z\\\2))\)\\\\\d+\\\\\1\\\\

              Are you kidding?
              I couldn’t even see it – at first – in (?-s)\(.+?)\)

              BTW, what made me screw it up in the first place is the simplicity of the OP’s request. It was so easy that I just typed it and didn’t feel the need to test it. Had I tested it, it would have been obvious there was a problem. Again, apologies to the OP – when we give answers here, we hope they are good answers.

              1 Reply Last reply Reply Quote 3
              • M
                Munter Göök @Alan Kilborn
                last edited by Mar 23, 2021, 11:21 AM

                Thanks Alan! Now it works perfectly.
                The reason I couldn’t find out whyit’s working or not is that is my learning curve. I simply don’t have the time nor the experience needed to understand the syntax. So then I was crying out for help. Anyways this solved my problem and I am greateful.

                It would be nice if succesful solutions like this were to be found in some archive or why not implemented in N++.

                Thanks again!

                A R 2 Replies Last reply Mar 23, 2021, 11:51 AM Reply Quote 1
                • A
                  Alan Kilborn @Munter Göök
                  last edited by Mar 23, 2021, 11:51 AM

                  @Munter-Göök said in How can I find strings between "(" and ")" with any number of characters in between?:

                  It would be nice if succesful solutions like this were to be found in some archive

                  Now it can be found right here on the forum (a form of archive).

                  why not implemented in N++

                  Well, what would be implemented?
                  I mean, your search is rather particular to your need, so N++ can’t implement the needs of everyone (those types of needs).

                  I simply don’t have the time nor the experience needed to understand the syntax

                  Understood, but if you continue to have such needs and continually come here to ask questions and receive answers, you’ll eventually be told that you need to start learning for yourself. That’s because this is a Notepad++ forum, not a data-manipulation via regular expression forum.

                  1 Reply Last reply Reply Quote 1
                  • A
                    Alan Kilborn
                    last edited by Mar 23, 2021, 12:00 PM

                    be told that you need to start learning for yourself

                    And, if you want to get a jump on the learning, reading this is a great starter: https://community.notepad-plus-plus.org/topic/15765/faq-desk-where-to-find-regex-documentation

                    1 Reply Last reply Reply Quote 0
                    • R
                      Rayran Brito @Munter Göök
                      last edited by Feb 12, 2024, 3:33 PM

                      @Munter-Göök said in How can I find strings between "(" and ")" with any number of characters in between?:

                      Thanks Alan! Now it works perfectly.
                      The reason I couldn’t find out whyit’s working or not is that is my learning curve. I simply don’t have the time nor the experience needed to understand the syntax. So then I was crying out for help. Anyways this solved my problem and I am greateful.

                      It would be nice if succesful solutions like this were to be found in some archive or why not implemented in N++.

                      Thanks again!

                      I appreciate @Alan-Kilborn for answering and @Munter-Göök for inquiring.
                      I’m grateful that you guys were able to solve my problem! This demonstrates the forum’s significance to those who are facing similar issues. 🤜🤛

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