I am not too familiar with regex and so I been having some getting this to work with Apple’s NSRegularExpression
I am trying to remove words in parentheses or brackets…
For example:
NSString *str = @”How do you (remove parentheses words) within a string using”
resulting string should be: @”How do you within a string using”
Thanks you!!!
Search for
and replace with nothing.
As a verbose regex:
This will work fine if parentheses are correctly balanced and unnested. If parentheses can be nested
(like this (for example)), then you need to re-run the replace until there are no further matches, since only the innermost parentheses will be matched in each run.*To remove brackets, do the same with
\[[^[\]]*\], for braces\{[^{}]*\}.With conditional expressions you could do all three at once, but the regex looks ugly, doesn’t it?
However, I’m not sure if NSRegularExpression can handle conditionals. Probably not. Explanation of this monster:
*You can’t match arbitrarily nested parentheses (since these constructs are no longer regular) with regular expressions, so that’s not a limitation of this regex in particular but of regexes in general.