且构网

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

找到两个字符串之间的所有子

更新时间:2022-12-29 10:35:02

 私人的IEnumerable<串GT; GetSubStrings(字符串输入,字符串的开始,结束串)
{
正则表达式R =新的正则表达式(Regex.Escape(开始)+(*)?+ Regex.Escape(结束));
MatchCollection匹配= r.Matches(输入);
的foreach(赛的比赛在比赛中)
收益率的回报match.Groups [1] .value的;
}


I need to get all substrings from string.
For ex:

StringParser.GetSubstrings("[start]aaaaaa[end] wwwww [start]cccccc[end]", "[start]", "[end]");

that returns 2 string "aaaaaa" and "cccccc" Suppose we have only one level of nesting. Not sure about regexp, but I think it will be userful.

private IEnumerable<string> GetSubStrings(string input, string start, string end)
{
    Regex r = new Regex(Regex.Escape(start) + "(.*?)" + Regex.Escape(end));
    MatchCollection matches = r.Matches(input);
    foreach (Match match in matches)
        yield return match.Groups[1].Value;
}