c#regex?文件中的文本字符串并排序为可搜索的数组?

时间:2021-05-18 21:21:35

I Have the following text from a file:

我有一个文件中的以下文字:

{"players":[{"i":11,"p":0,"a":3186,"n":"IanHx","f":1,"ps":0,"pd":0,"bc":0},{"i":12,"p":0,"a":115,"n":"LoZtamnik","f":1,"ps":0,"pd":0,"bc":0},{"i":58,"p":0,"a":156,"n":"Mr701","f":2,"ps":0,"pd":0,"bc":0},{"i":59,"p":0,"a":156,"n":"B0NE4","f":2,"ps":0,"pd":0,"bc":0},{"i":64,"p":0,"a":324,"n":"5teveJ","f":1,"ps":0,"pd":0,"bc":0}],[.......

What i am trying to do is parse the text, to end up with a array for each bit of data between {...}

我要做的是解析文本,最后得到一个数组,用于{...}之间的每一位数据

so final result would look like:

所以最终结果如下:

i=11
p=0
a=3186
n=IanHx
f=1
ps=0
pd=0
bc=0

then i can store these in a database

然后我可以将它们存储在数据库中

so far i have something like this:

到目前为止,我有这样的事情:

string contents = System.IO.File.ReadAllText("local text file"); //load string contents with text file
Regex regex1 = new Regex("\"players\":\\[(?<players>.*)\\]"); //find the players section "players":[.......]
Match match1 = regex1.Match(contents); //load match1    
Regex regex2 = new Regex("{(?<player>([^}]*))}"); // then break down each player {....}
MatchCollection match2 = regex2.Matches(match1.Groups["players"].Value); //load match2 with each player

then i get stuck trying to split the match string[] somehow and looking at it may be overcomplicating it?

然后我陷入困境试图以某种方式拆分匹配字符串[],看着它可能会过度复杂吗?

any pointer to an easier solution to the data parsing

指向数据解析的更简单解决方案的任何指针

Thanks

谢谢

2 个解决方案

#1


3  

The data contained in your file are in JSON format. JSON is quite simple to read, if formatted correctly. If I reformat your input the structure becomes clearer:

您文件中包含的数据采用JSON格式。如果格式正确,JSON很容易阅读。如果我重新格式化您的输入,结构会变得更清晰:

{
  "players": [
    {
      "i": 11,
      "p": 0,
      "a": 3186,
      "n": "IanHx",
      "f": 1,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 12,
      "p": 0,
      "a": 115,
      "n": "LoZtamnik",
      "f": 1,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 58,
      "p": 0,
      "a": 156,
      "n": "Mr701",
      "f": 2,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 59,
      "p": 0,
      "a": 156,
      "n": "B0NE4",
      "f": 2,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 64,
      "p": 0,
      "a": 324,
      "n": "5teveJ",
      "f": 1,
      "ps": 0,
      "pd": 0,
      "bc": 0
    }
  ]
}

In JSON, anything enclosed in [ ] denotes a collection and anything enclosed in { } denotes an object. So, you can see that you have a collection called players which contains 5 objects (since there are 5 pairs of { } within players [ ]) with 8 properties each. If you think of these in C# terms, you would have a class called Player with those 8 properties, and a List<Player> to hold each Player instance. You could then take the JSON data and deserialize them into their C# counterparts so you can manipulate them as you see fit, as Dave Bish has pointed out in his answer.

在JSON中,[]中包含的任何内容表示集合,{}中包含的任何内容表示对象。所以,你可以看到你有一个叫做玩家的集合,其中包含5个对象(因为玩家[]内有5对{}),每个属性有8个属性。如果您在C#术语中考虑这些,那么您将拥有一个名为Player且具有这8个属性的类,以及一个List 来保存每个Player实例。然后你就可以获取JSON数据并将它们反序列化为它们的C#对应物,这样你就可以按照你认为合适的方式对它们进行操作,正如Dave Bish在他的回答中指出的那样。

There's a very easy way to create the C# classes from your JSON data automatically:

有一种非常简单的方法可以自动从JSON数据创建C#类:

  • Create a new class in your project, name it however you want and clear all of it's content
  • 在项目中创建一个新类,根据需要命名并清除所有内容
  • Copy the JSON data (either from my example or yours)
  • 复制JSON数据(来自我的示例或您的示例)
  • Go back to the class and click Edit -> Paste Special -> Paste JSON As Classes
  • 返回到该类,然后单击编辑 - >选择性粘贴 - >将JSON粘贴为类

Voila. Visual Studio's got your back. You should now see something along these lines:

瞧。 Visual Studio让你回来了。您现在应该看到以下内容:

public class Rootobject
{
    public Player[] players { get; set; }
}

public class Player
{
    public int i { get; set; }
    public int p { get; set; }
    public int a { get; set; }
    public string n { get; set; }
    public int f { get; set; }
    public int ps { get; set; }
    public int pd { get; set; }
    public int bc { get; set; }
}

You can then do whatever suits your scenario best, e.g. add the System.Collections.Generic namespace so you can make Player[] a List<Player> instead so on.

然后,您可以做任何最适合您情景的事情,例如:添加System.Collections.Generic命名空间,以便您可以使Player []成为List 而不是如此。

Now, to manipulate the JSON data and deserialize them into the C# class we've just created, you can use the excellent Json.NET library. To add it, right click your application from the solution explorer and click "Manage NuGet Packages...". Type "Json.NET" in the search box and install it.

现在,要操纵JSON数据并将它们反序列化为我们刚刚创建的C#类,您可以使用优秀的Json.NET库。要添加它,请右键单击解决方案资源管理器中的应用程序,然后单击“管理NuGet包...”。在搜索框中键入“Json.NET”并进行安装。

Once you have that in place, add the Newtonsoft.Json namespace and you're good to go. You can now use Json.NET's DeserializeObject<T>() method to deserialize the JSON data into the C# classes we've created:

一旦你有了它,添加Newtonsoft.Json命名空间,你很高兴。您现在可以使用Json.NET的DeserializeObject ()方法将JSON数据反序列化为我们创建的C#类:

//i've hardcoded the JSON data here, obviously you would extract them from your file
var jsonData = @"{""players"":[{""i"":11,""p"":0,""a"":3186,""n"":""IanHx"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":12,""p"":0,""a"":115,""n"":""LoZtamnik"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":58,""p"":0,""a"":156,""n"":""Mr701"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":59,""p"":0,""a"":156,""n"":""B0NE4"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":64,""p"":0,""a"":324,""n"":""5teveJ"",""f"":1,""ps"":0,""pd"":0,""bc"":0}]}";

//deserialize the JSON into the C# class we created and store it in myPlayerData
var myPlayerData = JsonConvert.DeserializeObject<Rootobject>(jsonData);

//you can now do stuff such as..
foreach(Player player in myPlayerData.players)
{
    MessageBox.Show(string.Format("Player {0} has an i of {1} and an a of {2}", player.n, player.i, player.a));
}

#2


4  

Your far better off trying to deserialize this with Json.Net from NuGet

你试图用NuGet的Json.Net反序列化它会好得多

Define some classes to match your file structure:

定义一些类以匹配您的文件结构:

public class Root
{
    public List<Something> players {get; set;}
}

public class Something
{
    public string i {get; set;}
    public string p {get; set;}
    public string a {get; set;}
    public string n {get; set;}
    public string f {get; set;}
    public string ps {get; set;}
    public string pd {get; set;}
    public string bc {get; set;}
}

use Json.Net to crunch:

使用Json.Net来处理:

var json = @"{""players"":[{""i"":11,""p"":0,""a"":3186,""n"":""IanHx"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":12,""p"":0,""a"":115,""n"":""LoZtamnik"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":58,""p"":0,""a"":156,""n"":""Mr701"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":59,""p"":0,""a"":156,""n"":""B0NE4"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":64,""p"":0,""a"":324,""n"":""5teveJ"",""f"":1,""ps"":0,""pd"":0,""bc"":0}]}";

var data = JsonConvert.DeserializeObject<Root>(json);

#1


3  

The data contained in your file are in JSON format. JSON is quite simple to read, if formatted correctly. If I reformat your input the structure becomes clearer:

您文件中包含的数据采用JSON格式。如果格式正确,JSON很容易阅读。如果我重新格式化您的输入,结构会变得更清晰:

{
  "players": [
    {
      "i": 11,
      "p": 0,
      "a": 3186,
      "n": "IanHx",
      "f": 1,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 12,
      "p": 0,
      "a": 115,
      "n": "LoZtamnik",
      "f": 1,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 58,
      "p": 0,
      "a": 156,
      "n": "Mr701",
      "f": 2,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 59,
      "p": 0,
      "a": 156,
      "n": "B0NE4",
      "f": 2,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 64,
      "p": 0,
      "a": 324,
      "n": "5teveJ",
      "f": 1,
      "ps": 0,
      "pd": 0,
      "bc": 0
    }
  ]
}

In JSON, anything enclosed in [ ] denotes a collection and anything enclosed in { } denotes an object. So, you can see that you have a collection called players which contains 5 objects (since there are 5 pairs of { } within players [ ]) with 8 properties each. If you think of these in C# terms, you would have a class called Player with those 8 properties, and a List<Player> to hold each Player instance. You could then take the JSON data and deserialize them into their C# counterparts so you can manipulate them as you see fit, as Dave Bish has pointed out in his answer.

在JSON中,[]中包含的任何内容表示集合,{}中包含的任何内容表示对象。所以,你可以看到你有一个叫做玩家的集合,其中包含5个对象(因为玩家[]内有5对{}),每个属性有8个属性。如果您在C#术语中考虑这些,那么您将拥有一个名为Player且具有这8个属性的类,以及一个List 来保存每个Player实例。然后你就可以获取JSON数据并将它们反序列化为它们的C#对应物,这样你就可以按照你认为合适的方式对它们进行操作,正如Dave Bish在他的回答中指出的那样。

There's a very easy way to create the C# classes from your JSON data automatically:

有一种非常简单的方法可以自动从JSON数据创建C#类:

  • Create a new class in your project, name it however you want and clear all of it's content
  • 在项目中创建一个新类,根据需要命名并清除所有内容
  • Copy the JSON data (either from my example or yours)
  • 复制JSON数据(来自我的示例或您的示例)
  • Go back to the class and click Edit -> Paste Special -> Paste JSON As Classes
  • 返回到该类,然后单击编辑 - >选择性粘贴 - >将JSON粘贴为类

Voila. Visual Studio's got your back. You should now see something along these lines:

瞧。 Visual Studio让你回来了。您现在应该看到以下内容:

public class Rootobject
{
    public Player[] players { get; set; }
}

public class Player
{
    public int i { get; set; }
    public int p { get; set; }
    public int a { get; set; }
    public string n { get; set; }
    public int f { get; set; }
    public int ps { get; set; }
    public int pd { get; set; }
    public int bc { get; set; }
}

You can then do whatever suits your scenario best, e.g. add the System.Collections.Generic namespace so you can make Player[] a List<Player> instead so on.

然后,您可以做任何最适合您情景的事情,例如:添加System.Collections.Generic命名空间,以便您可以使Player []成为List 而不是如此。

Now, to manipulate the JSON data and deserialize them into the C# class we've just created, you can use the excellent Json.NET library. To add it, right click your application from the solution explorer and click "Manage NuGet Packages...". Type "Json.NET" in the search box and install it.

现在,要操纵JSON数据并将它们反序列化为我们刚刚创建的C#类,您可以使用优秀的Json.NET库。要添加它,请右键单击解决方案资源管理器中的应用程序,然后单击“管理NuGet包...”。在搜索框中键入“Json.NET”并进行安装。

Once you have that in place, add the Newtonsoft.Json namespace and you're good to go. You can now use Json.NET's DeserializeObject<T>() method to deserialize the JSON data into the C# classes we've created:

一旦你有了它,添加Newtonsoft.Json命名空间,你很高兴。您现在可以使用Json.NET的DeserializeObject ()方法将JSON数据反序列化为我们创建的C#类:

//i've hardcoded the JSON data here, obviously you would extract them from your file
var jsonData = @"{""players"":[{""i"":11,""p"":0,""a"":3186,""n"":""IanHx"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":12,""p"":0,""a"":115,""n"":""LoZtamnik"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":58,""p"":0,""a"":156,""n"":""Mr701"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":59,""p"":0,""a"":156,""n"":""B0NE4"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":64,""p"":0,""a"":324,""n"":""5teveJ"",""f"":1,""ps"":0,""pd"":0,""bc"":0}]}";

//deserialize the JSON into the C# class we created and store it in myPlayerData
var myPlayerData = JsonConvert.DeserializeObject<Rootobject>(jsonData);

//you can now do stuff such as..
foreach(Player player in myPlayerData.players)
{
    MessageBox.Show(string.Format("Player {0} has an i of {1} and an a of {2}", player.n, player.i, player.a));
}

#2


4  

Your far better off trying to deserialize this with Json.Net from NuGet

你试图用NuGet的Json.Net反序列化它会好得多

Define some classes to match your file structure:

定义一些类以匹配您的文件结构:

public class Root
{
    public List<Something> players {get; set;}
}

public class Something
{
    public string i {get; set;}
    public string p {get; set;}
    public string a {get; set;}
    public string n {get; set;}
    public string f {get; set;}
    public string ps {get; set;}
    public string pd {get; set;}
    public string bc {get; set;}
}

use Json.Net to crunch:

使用Json.Net来处理:

var json = @"{""players"":[{""i"":11,""p"":0,""a"":3186,""n"":""IanHx"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":12,""p"":0,""a"":115,""n"":""LoZtamnik"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":58,""p"":0,""a"":156,""n"":""Mr701"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":59,""p"":0,""a"":156,""n"":""B0NE4"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":64,""p"":0,""a"":324,""n"":""5teveJ"",""f"":1,""ps"":0,""pd"":0,""bc"":0}]}";

var data = JsonConvert.DeserializeObject<Root>(json);