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

前端之家收集整理的这篇文章主要介绍了具有相同模式的字符串,使用正则表达式组的提取和替换的案例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

提取字符串: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.jb51.cc/article/p-toaotdxm-vg.html http://www.jb51.cc/article/p-accvysvj-vg.html)

原文链接:https://www.f2er.com/regex/360569.html

猜你在找的正则表达式相关文章