且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用NSRegularExpression删除字符串中的括号词?

更新时间:2023-02-21 15:25:02

搜索

\([^()]*\)

然后什么也不要替换.

作为详细的正则表达式:

\(      # 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.

* 您不能将任意嵌套的括号(因为这些构造不再是正则表达式)与正则表达式进行匹配,因此,这不仅不是此regex的限制,而是一般的正则表达式的限制.>

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:

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

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?

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

However, I'm not sure if NSRegularExpression can handle conditionals. Probably not. Explanation of this monster:

(?:           # 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.

*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.