获得字符串中开始和结束字符串中间得值

 1 /// <summary>
 2         /// 获得字符串中开始和结束字符串中间得值
 3         /// </summary>
 4         /// <param name="str">字符串</param>
 5         /// <param name="s">开始</param>
 6         /// <param name="e">结束</param>
 7         /// <returns></returns> 
 8         public static string GetMidValue(string str, string s, string e)
 9         {
10             Regex rg = new Regex("(?<=(" + s + "))[.\s\S]*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline);
11             return s.Trim('\') + rg.Match(str).Value + e.Trim('\');
12         }
 1 List<string> listdetail=new List<string>();
 2             string strhtml = "testaaa{aaa}testbbb{bbb}testccc{ccc}";
 3             for (int i = 0; i < 30; i++)
 4             {
 5                 string find = GetMidValue(strhtml, "{", "}");
 6                 if (find != "{}")
 7                 {
 8                     listdetail.Add(find);
 9                     if (i == 0)
10                     {
11                         strhtml = strhtml.Replace(find, "<start>");
12                     }
13                     else
14                     {
15                         strhtml = strhtml.Replace(find, "<end>");
16                     }
17                 }
18                 else
19                 {
20                     break;
21                 }
22             }
23             string end = strhtml;
原文地址:https://www.cnblogs.com/Jeremy2001/p/7448851.html