C# 字符串模式匹配

发布于 2024-11-13 14:33:29 字数 1076 浏览 2 评论 0原文

我有 2 个 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).

I have 2 lists of strings in C# showing the players who have joined and left a particular game. I'm trying to attempt to determine who is still in the game by matching both lists and eliminating the entries of those who have left the game. Please suggest a easy and pain free algorithm to go about doing this. My current code is as follows

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

        }

Here is some sample input :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

满栀 2024-11-20 14:33:29

您是在问如何获得您的两个名单,或者在您已经获得两个名单后如何找到当前的球员?

第二部分可以用 Linq 完成...

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

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

编辑为了回应您的评论,再次阅读您的问题后,显然上述内容将不起作用,因为您正在以一种奇怪的方式构建列表。

您将整个字符串存储在列表中,例如user_1已离开游戏,您可能应该做的只是存储用户名。如果你纠正了这个问题,那么上面的代码就可以完全满足你的要求。

一个完整的例子:

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

Are you asking how to get your two lists, or how to find the current players after you've already got the two lists?

The second part can be done with Linq....

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

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

EDIT In response to your comment, having read your question again then obviously the above won't work because you are building your lists in a weird way.

You are storing the whole string in the list, e.g. user_1 has left the game, what you should probably be doing is just storing the user name. If you correct this then the above code does exactly what you want.

A full example:

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"); 
}
后知后觉 2024-11-20 14:33:29

相交除了是你的朋友。

另外,如果这是列表的唯一目的,请考虑使用类似 HashSet 相反。

Intersect and Except are your friends.

Also, if this is the sole purpose of the lists, consider using something like HashSet instead.

涫野音 2024-11-20 14:33:29

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

use linq and regex:

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();
倾听心声的旋律 2024-11-20 14:33:29

首先,您需要提取玩家姓名,以便计算差异:

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

First you need to extract the player names so you can calculate the difference:

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

Now calculate the difference:

var playersStillInGame = joinedNames.Except(leftNames);
霓裳挽歌倾城醉 2024-11-20 14:33:29
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);
}
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);
}
煮茶煮酒煮时光 2024-11-20 14:33:29

使用List.Find()怎么样?

链接 1
此处链接 2

How about using List.Find()?

Link 1 here
Link 2 here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文