其他分享
首页 > 其他分享> > 根据正则表达式来获取开始符和结束符之间的字符串

根据正则表达式来获取开始符和结束符之间的字符串

作者:互联网

具体实现

        /// <summary>
        /// 截取某两个字符中间的字符串
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <param name="s">开始字符</param>
        /// <param name="e">结束字符</param>
        /// <returns></returns>
        public string SubBetweenStr(string str, string s, string e)
        {
            Regex rg = new Regex($"(?<=({s}))[.\\s\\S]*?(?=({e}))",RegexOptions.Multiline|RegexOptions.Singleline);
            return rg.Match(str).Value;
        }

使用

var a = "123456789sdasdadwrdfcasfas\r\n";
var b = SubBetweenStr(a, "123", "\r\n");
Console.WriteLine(b);

 

结果

456789sdasdadwrdfcasfas

 

标签:Regex,字符,string,正则表达式,var,SubBetweenStr,结束符,字符串
来源: https://www.cnblogs.com/jiatree/p/16386044.html