[正则表达式]

1.去除括弧内的字符

using System.Text.RegularExpressions;

var majorname = "ATM0243(Dispaly)".Replace("(", "(").Replace(")",")");

majorname = Regex.Replace(majorname.Replace("(", "(").Replace(")", ")"), @"\([^\(]*\)", "");

最终得到结果:ATM0243

2.截取括号里的字符串

str = System.Text.RegularExpressions.Regex.Replace(@"带括号的(ddd)字符串", @"(.*\()(.*)(\).*)", "$2");//

3.如何去掉字符串首尾特定字符(串)?

///<summary>
 /// 截前后字符(串)
 ///</summary>
 ///<param name="val">原字符串</param>
 ///<param name="str">要截掉的字符串</param>
 ///<param name="all">是否贪婪</param>
 ///<returns></returns>
        private string GetString(string val,string str,bool all)
        {
            return Regex.Replace(val, @"(^(" + str + ")" + (all ? "*" : "") + "|(" + str + ")"+(all ? "*" : "") + "$)", "");
        }

4.利用正则表达式取出括号中的内容,多个括号

   string source = "(abc)de(fg)hi(jk)";
            Regex reg = new Regex(@"(?is)(?<=\()[^\)]+(?=\))");
            MatchCollection mc = reg.Matches(source);
            foreach (Match m in mc)
            {
                MessageBox.Show(m.Value);
            }

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 鲸 设计师: meimeiellie
应支付0元
点击重新获取
扫码支付

支付成功即可阅读