I have a couple of strings I’d like to remove unclosed brackets such as (
or [
"A Pro Webcam ((Good text) [[Good Text] more texts"
"Text (Example (Good text) [Good Text] more texts"
I have tried using this with php with no luck
((?:[^)(]*(?R)?)*+)
2
Answers
Match pairs of
(
and)
and[
and]
using a stack. If there is a mismatch in such a pair of characters OR if the characters are not available(meaning hanging closed parentheses), collect all such guys in array to be skipped later when creating a clean string.Online Demo
You can use
See the regex demo. Replace with an empty string.
Details:
(?:(((?:[^()]++|(?1))*))|([(?:[^][]++|(?2))*]))(*SKIP)(*F)
– a string between paired(
and)
or between[
and]
|
– or[][()]
– a[
,]
,(
or)
.