参考代码
/// <summary>
/// 正则提取中间字符
/// </summary>
/// <param name="source">原字符串</param>
/// <param name="startStr">开始字符</param>
/// <param name="endStr">结束字符</param>
/// <returns>中间字符</returns>
public static 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;
}
也可以写成拓展方法放在静态类里直接在string调用
public static class MyStringExtend
{
public static string SubstringSingle(this string source, string startStr, string endStr)
{
Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline);
return rg.Match(source).Value;
}
}