以严格的文本获取文本的特定部分

Sta*_*ser 5 .net c# string c#-4.0

假设有一个如下文字:

string str = @"stackoverflow(:stackoverflow)overstackflow(:stackoverflow)";

我希望得到大胆的领域.我想我必须在文本中找到"("和":"并在它们之间获取文本.不是吗?

有什么建议?

Tim*_*ter 6

也许用简单的string方法:

IList<String> foundStrings = new List<String>();
int currentIndex = 0;
int index = str.IndexOf("(", currentIndex);
while(index != -1)
{
    int start = index + "(".Length;
    int colonIndex = str.IndexOf(":", start);
    if (colonIndex != -1)
    {
        string nextFound = str.Substring(start, colonIndex - start);
        foundStrings.Add(nextFound);
    }
    currentIndex = start;
    index = str.IndexOf("(", currentIndex);
}
Run Code Online (Sandbox Code Playgroud)

演示