如何在字符串中获取[[string]]内的字符串? (C#)

时间:2013-06-01 19:23:32

标签: c# string parsing

正如您在C#中看到的那样,语句在括号内{} 现在如果我们有一个字符串在[[]]中包含多个字符串,那该怎么办呢。

示例输入输出:

string s = "this is the string I am talking about [[and the text inside]] but don't      forget about other [[ones like this]].";

string[] insideBrackets;

insideBrackets[0] will be **and the text inside**
insideBrackets[1] will be **ones like this**

顺便说一下,我有字符串拆分和indexOf,但它们不能正常工作。 感谢无论谁回答,希望这是一个很好的问题竖起大拇指:)

1 个答案:

答案 0 :(得分:9)

您可以使用Regex

string s = "this is the string I am talking about [[and the text inside]] but don't      forget about other [[ones like this]].";

var insideBrackets = Regex.Matches(s, @"\[\[(.+?)\]\]").Cast<Match>()
                        .Select(m => m.Groups[1].Value)
                        .ToArray();