加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

具有相同模式的字符串,使用正则表达式组的提取和替换的案例

发布时间:2020-12-14 01:22:11 所属栏目:百科 来源:网络整理
导读:对具有相同模式的字符串内不同的组的提取怎样做呢,我是这样做的: 提取字符串:sourcetext:{name:john,data:[1,2,3],name:marry,data:[4,5,6]} 代码: Regex reg = new Regex(@"data:[([w|.|,]{1,})]",RegexOptions.IgnoreCase);MatchCollection matches

对具有相同模式的字符串内不同的组的提取怎样做呢,我是这样做的:

提取字符串:sourcetext:{name:john,data:[1,2,3],name:marry,data:[4,5,6]}

代码:

Regex reg = new Regex(@"data:[([w|.|,]{1,})]",RegexOptions.IgnoreCase);
MatchCollection matches = reg.Matches(series);
foreach (Match match in matches)
{

   GroupCollection groups = match.Groups;
   for (int j = 0; j < groups.Count; j++)
   {
     string weightjsonInKgMode = covertToKg(groups[j].Value.Replace("data:[","").Replace("]",""));
     string regModel = groups[j].Value;
     regModel = regModel.Replace("[","[").Replace("]","]");
     series = Regex.Replace(series,regModel,weightjsonInKgMode);
   }
}

结果:

series中的“1,3”,和“4,6”,将分别被weightjsonInKgMode字符串“0.001,0.002,0.003”和“0.004,0.0045,0.0046”所代替

(参考:http://www.52php.cn/article/p-toaotdxm-vg.html http://www.52php.cn/article/p-accvysvj-vg.html)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读