对具有相同模式的字符串内不同的组的提取怎样做呢,我是这样做的:
提取字符串: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)