C# 如何获取所有标记用户的循环
我正在尝试从ASP.NET中的字符串中获取所有标记的用户 例如,字符串“Hello my name is@Naveh,my friend is name@Amit”,我希望它返回我“Naveh”和“Amit”,这样我就可以向每个用户发送一个通知方法,就像代码后面的循环一样 我所知道的捕捉这些字符串的唯一方法是使用类似这样的“Replace”方法:(但这当然只适用于编辑)C# 如何获取所有标记用户的循环,c#,asp.net,loops,C#,Asp.net,Loops,我正在尝试从ASP.NET中的字符串中获取所有标记的用户 例如,字符串“Hello my name is@Naveh,my friend is name@Amit”,我希望它返回我“Naveh”和“Amit”,这样我就可以向每个用户发送一个通知方法,就像代码后面的循环一样 我所知道的捕捉这些字符串的唯一方法是使用类似这样的“Replace”方法:(但这当然只适用于编辑) Regex.Replace(注释,@“@([\S]+)”,@“”) 你不能那样循环那些字符串。如何循环代码中所有标记的用户
Regex.Replace(注释,@“@([\S]+)”,@“”)
你不能那样循环那些字符串。如何循环代码中所有标记的用户 您可以使用Regex.Matches来获取MatchCollection对象,并使用foreach从中获取战利品 您可以使用Regex.Matches来获取MatchCollection对象,并使用foreach从中获取战利品 您可能应该使用Regex.Match 例如
您可能应该使用Regex.Match 例如
Regex.Replace(comment, @"@([\S]+)", @"<a href=""../sellingProfile.aspx?name=$1""><b>$1</b></a>")
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();