How can I find strings between "(" and ")" with any number of characters in between?
-
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. -
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. -
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…
Thanks anyway!
-
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.
-
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
-
@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.
-
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!
-
@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.
-
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
-
@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. 🤜🤛