簡體   English   中英

C# 字符串模式匹配

[英]C# String Pattern Matching

我在 C# 中有 2 個字符串列表,顯示了加入和離開特定游戲的玩家。 我試圖通過匹配兩個列表並消除那些離開游戲的人的條目來嘗試確定誰仍然在游戲中。 請向 go 建議一個簡單且無痛的算法來執行此操作。 我當前的代碼如下

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).

您是在問如何獲得您的兩個列表,或者在您已經獲得兩個列表之后如何找到當前玩家?

第二部分可以用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"); 
}

相交除外是你的朋友。

此外,如果這是列表的唯一目的,請考慮使用類似HashSet的東西。

使用 List.Find() 怎么樣? 此處鏈接 1 此處鏈接 2

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

首先,您需要提取玩家名稱,以便計算差異:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM