转:C# 正则表达式提取指定文本内的内容
C# 正则表达式提取指定文本内的内容_c#正则表达式提取文本-CSDN博客
C#正则提取字符串(一个或多个)_c#正则表达式提取字符串-CSDN博客
/// <summary> /// 截取字符串中开始和结束字符串中间的字符串 /// </summary> /// <param name="source">源字符串</param> /// <param name="startStr">开始字符串</param> /// <param name="endStr">结束字符串</param> /// <returns>中间字符串</returns> public string SubstringSingle(string source, string startStr, string endStr) { Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline); return rg.Match(source).Value; } /// <summary> /// (批量)截取字符串中开始和结束字符串中间的字符串 /// </summary> /// <param name="source">源字符串</param> /// <param name="startStr">开始字符串</param> /// <param name="endStr">结束字符串</param> /// <returns>中间字符串</returns> public List<string> SubstringMultiple(string source, string startStr, string endStr) { Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline); MatchCollection matches = rg.Matches(source); List<string> resList=new List<string>(); foreach (Match item in matches) resList.Add(item.Value); return resList; }
以上可以从html中代码中提取一些数据,如从table中取表数据等。