[英]How to get a loop of all tagged users
我正在尝试从ASP.NET中的字符串中获取所有标记的用户,例如字符串“你好,我的名字是@Naveh,我的朋友名为@Amit”,我希望它返回我的“ Naveh”和“ Amit”我可以向每个用户发送通知方法的方法,例如后面代码的循环。
我知道捕获这些字符串的唯一方法是通过像这样的'Replace'方法:(但这当然仅对编辑有用)
Regex.Replace(comment, @"@([\S]+)", @"<a href=""../sellingProfile.aspx?name=$1""><b>$1</b></a>")
您不能像这样循环这些字符串。 如何在后面的代码中循环所有加标签的用户?
您可能应该使用Regex.Match。
例如
string pat = @"@([a-z]+)";
string src = "Hello my name is @Naveh and my friend is named @Amit";
string output = "";
// Instantiate the regular expression object.
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
// Match the regular expression pattern against a text string.
Match m = r.Match(src);
while (m.Success)
{
string matchValue = m.Groups[1].Value; //m.Groups[0] = "@Name". m.Groups[1] = "Name"
output += "Match: " + matchValue + "\r\n";
m = m.NextMatch();
}
Console.WriteLine(output);
Console.ReadLine();
您可以使用Regex.Matches获取MatchCollection对象,并使用foreach对其进行掠夺。 MSDN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.