码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-08 09:11:29      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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

提取字符串: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,2,3”,和“4,5,6”,将分别被weightjsonInKgMode字符串“0.001,0.002,0.003”和“0.004,0.0045,0.0046”所代替

(参考:http://blog.csdn.net/goodshot/article/details/6676349   http://blog.csdn.net/goodshot/article/details/44935521)

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

标签:

原文地址:http://blog.csdn.net/goodshot/article/details/44935611

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!