How to remove words in brackets in a string using NSRegularExpression?

I am not very familiar with regular expression, and therefore I had some desire to work with Apple NSRegularExpression

I am trying to remove words in brackets or brackets ...

For instance:

NSString * str = @ "How do you (delete words in brackets) inside a string using

the resulting string should be: @ "How can you use a string with

Thanks you!!!

+2
source share
2 answers

Search

\([^()]*\)

and do not replace anything.

Like a verbose regular expression:

\(      # match an opening parenthesis
[^()]*  # match any number of characters except parentheses
\)      # match a closing parenthesis

, . (like this (for example)), , , . *

, \[[^[\]]*\], \{[^{}]*\}.

, , ?

(?:(\()|(\[)|(\{))[^(){}[\]]*(?(1)\))(?(2)\])(?(3)\})

, NSRegularExpression . . :

(?:           # start of non-capturing group (needed for alternation)
 (\()         # Either match an opening paren and capture in backref #1
 |            # or
 (\[)         # match an opening bracket into backref #2
 |            # or
 (\{)         # match an opening brace into backref #3
)             # end of non-capturing group
[^(){}[\]]*   # match any number of non-paren/bracket/brace characters
(?(1)\))      # if capturing group #1 matched before, then match a closing parenthesis
(?(2)\])      # if #2 matched, match a closing bracket
(?(3)\})      # if #3 matched, match a closing brace.

* ( ) , , , .

+5

objectice-c regex, PCRE :

s/\[.*?\]|\(.*?\)|\{.*?\}//g

.

+2

All Articles