开发者

Removing text in all kinds of braces

开发者 https://www.devze.com 2022-12-30 05:53 出处:网络
I have a string \"hello [world] this {is} a (test)\" I want to remove all text in braces, e.g. returning \"hellothisa\". But only if the brac开发者_开发知识库es match.

I have a string "hello [world] this {is} a (test)" I want to remove all text in braces, e.g. returning "hello this a". But only if the brac开发者_开发知识库es match.

Anyone have a nice neat solution?


You can use a regular expression:

s = Regex.Replace(s, @"\s*?(?:\(.*?\)|\[.*?\]|\{.*?\})", String.Empty);

The \s*? matches any white space before the brackets.
The (?: ) is a non-matching parenthesis to group the conditions inside it.
The \(.*?\) is mathing parentheses with zero or more characters between them.

0

精彩评论

暂无评论...
验证码 换一张
取 消