1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 具有相同模式的字符串 使用正则表达式组的提取和替换的案例

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

时间:2023-11-12 06:33:58

相关推荐

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

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

提取字符串: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”所代替

(参考:/goodshot/article/details/6676349 /goodshot/article/details/44935521)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。