2017-10-11 15 views
0

タグ付きのユーザーをすべてASP.NETの文字列から取得しようとしています たとえば "Hello my name is @Naveh、私の友達は@Amit"という文字列です。私の "Naveh"と "Amit"を返してくれるように、私はそれらのユーザのそれぞれに通知メソッドを送ることができます。すべてのタグ付きユーザーのループを取得する方法

私はこれらの文字列をキャッチするために知っている唯一の方法は、そのような「交換する」方法である:(しかし、それは当然の編集のための唯一の良いです)

Regex.Replace(comment, @"@([\S]+)", @"<a href=""../sellingProfile.aspx?name=$1""><b>$1</b></a>") 

あなたができることを好きではないループそれらの文字列。コード内のタグ付きユーザーをすべてループバックするにはどうすればよいですか?

答えて

2

おそらくRegex.Matchを使用するべきです。

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

ありがとうございました。 – Naveh

0

Regex.Matchesを使用すると、MatchCollectionオブジェクトを取得し、foreachを使用してそのオブジェクトを盗むことができます。 MSDN

関連する問題