您的位置:首页 > 其它

正则使用的几种形态

2016-07-26 21:08 337 查看
1、判断是否匹配

   Regex re = new Regex("\\\\u[0123456789abcdef]{4}", RegexOptions.IgnoreCase);

   re.IsMatch(InputStr);

2、使用Match判断匹配并取匹配的值

   string strReg = @"((product_list).+?\})";//short开始的提取出来

   Regex re = new Regex(strReg, RegexOptions.IgnoreCase);

   if (re.Match(inputStr).Success)

   {

    sb.Append(re.Match(inputStr).Value);//获取匹配的值

   }

3、匹配多组,循环匹配的组

 string
strReg = @"((product_list).+?\})";

 Regex re = new Regex(strReg, RegexOptions.IgnoreCase);

 string res;

   MatchCollection mc = re.Matches(inputStr);

    foreach (Match ma in mc)

     {

       string aa = ma.Value;

       if (aa.Contains("DNH"))

        {

         str2=System.Text.RegularExpressions.Regex.Split(aa, ",");

         res = str2[1];

        }

     }

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: