2011-06-04 96 views
0

我有2個C#字符串列表,顯示已加入並離開特定遊戲的玩家。我試圖通過匹配兩個列表並且消除那些已經離開遊戲的人的條目來試圖確定誰還在遊戲中。請建議一個簡單而無痛的算法去做這件事。我當前的代碼如下所示C#字符串模式匹配

string input = inputTextBox.Text; 
     string[] lines = input.Split(new string[] {"\r\n", "\n"}, StringSplitOptions.None); 
     List<string> filteredinput = new List<string>(); 
     List<string> joinedlog = new List<string>(); 
     List<string> leftlog = new List<string>(); 

     for (int i = 0; i<lines.Length; i++) 
     { 
      if (lines[i].Contains("your game!")) 
      filteredinput.Add(lines[i]); 
     } 

     for (int i =0; i<filteredinput.Count; i++) 
     { 
      if (filteredinput[i].Contains("joined")) 
       joinedlog.Add(filteredinput[i]); 

      else if (filteredinput[i].Contains("left")) 
       leftlog.Add(filteredinput[i]); 

     } 

下面是一些示例輸入:

{SheIsSoScrewed}[Ping:|] has joined your game!. 
{AngeLa_Yoyo}[Ping:X] has joined your game!. 
{SheIsSoScrewed} has left your game!(4). 
+0

您可以顯示一些示例輸入嗎? – 2011-06-04 08:48:05

+0

確定我已經編輯它以顯示一些示例輸入 – paradox 2011-06-04 09:26:54

回答

2

你問如何讓你的兩個列表,或如何找到目前的球員你已經拿到了兩個名單後?

第二部分可以使用LINQ做....

List<string> joinedGame; 
List<string> leftGame; 

List<string> currentInGame 
      = joinedGame.Where(x => !leftGame.Contains(x)).ToList(); 

編輯在回答您的意見,已經看了一遍你的問題那麼很明顯,上面會不會因爲你的工作以奇怪的方式構建您的列表。

您正在將整個字符串存儲在列表中,例如, user_1 has left the game,你應該做的只是存儲用戶名。如果你糾正這個問題,那麼上面的代碼就是你想要的。

完整的例子:

var input = new List<string>() 
{ 
    "user_1 has joined the game", 
    "user_2 has joined the game", 
    "user_1 has left the game", 
    "user_3 has joined the game" 
}; 

var joined = new List<string>(); 
var left = new List<string>(); 

foreach(string s in input) 
{ 
    var idx = s.IndexOf(" has joined the game"); 
    if (idx > -1) 
    { 
     joined.Add(s.Substring(0, idx)); 
     continue; 
    } 

    idx = s.IndexOf(" has left the game"); 
    if (idx > -1) 
    { 
     left.Add(s.Substring(0, idx)); 
    } 
} 

var current = joined.Where(x => !left.Contains(x)).ToList(); 

foreach(string user in current) 
{ 
    Console.WriteLine(user + " is still in the game"); 
} 
+0

我要求後者 – paradox 2011-06-04 09:00:14

+0

好吧...所以我已經給你答案,然後 – fearofawhackplanet 2011-06-04 09:02:34

+0

我害怕沒有。它只顯示與joinedGame相同的輸出 – paradox 2011-06-04 09:12:29

0

使用LINQ和正則表達式:

var join=new Regex("joined.*?your game");  
var joinLog = (from l in lines where join.IsMatch(join) select l).ToList(); 

var left=new Regex("left.*?your game");  
var leftLog = (from l in lines where left.IsMatch(join) select l).ToList(); 
+0

我已經有加入和離開遊戲的人員的列表。我只需要找到誰仍然在比賽中 – paradox 2011-06-04 08:55:06

0

首先,你需要提取的球員的名字,所以你可以計算出差異:

var join=new Regex("{(.*)}[.*joined.*?your game"); 
var joinedNames = filteredinput.Select(l => join.Match(l)).Where(m => m.Success).Select(m => m.Groups[1]).Distinct(); 

var left=new Regex("{(.*)}[.*left.*?your game"); 
var leftNames = filteredinput.Select(l => left.Match(l)).Where(m => m.Success).Select(m => m.Groups[1]).Distinct(); 

現在計算差值:

var playersStillInGame = joinedNames.Except(leftNames); 
0
string input = inputTextBox.Text; 
string[] lines = input.Split(new string[] {"\r\n", "\n"}, StringSplitOptions.None); 

Regex joinedLeft = new Regex(@"\{([^{}]*)}.*? has (joined|left) your game!"); 
HashSet<string> inGame = new HashSet<string>(); 
foreach (string line in lines) 
{ 
    Match match = joinedLeft.Match(line); 
    if (!match.Success) 
     continue; 

    string name = match.Groups[1].Value; 
    string inOrOut = match.Groups[2].Value; 

    if (inOrOut == "joined") 
     inGame.Add(name); 
    else 
     inGame.Remove(name); 
}