日期:2014-05-17  浏览次数:20945 次

还是正则问题,绕不过去这道坎了
回复@展灵妙(1):辛苦了,为了开心活动//@展灵妙(1):回复@李恒昌(2):单位有活动,中午要留人手,@刘丽(3)和@王龙(4)别走了。

首先我想找出所有@的人的ID,@hjywyj已经帮我实现了

string str="字符串";     
var list = Regex.Matches(str, @"@([^(]+)\((\d+)\)").OfType<Match>().Select(t => new { name = t.Groups[1].Value, id = t.Groups[2].Value }).ToList();


然后我想把 @展灵妙(1)替换为<M 1>@展灵妙<M>,类似的也这样替换,貌似用Regex.Replace可以实现,希望得到帮助,谢谢。

------解决方案--------------------
 string str = "字符串";
            str = Regex.Replace(str, @"(@[^(]+)\((\d+)\)", "<M $2>$1<M>");