Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取所有标记用户的循环_C#_Asp.net_Loops - Fatal编程技术网

C# 如何获取所有标记用户的循环

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]+)”,@“”) 你不能那样循环那些字符串。如何循环代码中所有标记的用户

我正在尝试从ASP.NET中的字符串中获取所有标记的用户 例如,字符串“Hello my name is@Naveh,my friend is name@Amit”,我希望它返回我“Naveh”和“Amit”,这样我就可以向每个用户发送一个通知方法,就像代码后面的循环一样

我所知道的捕捉这些字符串的唯一方法是使用类似这样的“Replace”方法:(但这当然只适用于编辑)

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();