使用正则表达式拆分文本

时间:2022-08-08 21:31:04

i try to slit a text and put it into a dictionary , the problem i my text doesn't have a a clear structure : text :

我试图切开一个文本并将其放入字典,问题我的文字没有一个清晰的结构:文字:

{
   "about": "where I'm meant to be...",
   "bio": "Visit my official blog at:\n\nhttp://ABC.com/  \n\nAdd me on Twitter:\n\nhttp://www.ABC.com/ABC",
   "category": "Public figure",
   "is_published": true,
   "location": {
      "street": "",
      "city": "Los Angeles",
      "state": "CA",
      "country": "United States",
      "zip": ""
   },
   "talking_about_count": 254637,
   "username": "ABC",
   "website": "http://kimkardashian.celebuzz.com/\nhttp://www.twitter.com/kimkardashian\n",
   "were_here_count": 0,
   "id": "114696805612",
   "name": "ABC",
   "link": "http://www.ABC.com/ABC",
   "likes": 0,
   "cover": {
      "cover_id": "000000000",
      "source": "http://ABC.jpg",
      "offset_y": 0,
      "offset_x": 200
   }
}

As you see i have the "," as a delimiter , the problem is that there some composed objects like the :

如你所见,我有“,”作为分隔符,问题是有一些组合对象,如:

"location": {
      "street": "",
      "city": "Los Angeles",
      "state": "CA",
      "country": "United States",
      "zip": ""
   },

that's why I can't use the string.Split(' '); i heard about the regular expressions but I don't know how to use them Is there any solution to get those information separated into a dictionary or any other structure

这就是为什么我不能使用string.Split('');我听说过正则表达式,但我不知道如何使用它们是否有任何解决方案可以将这些信息分成字典或任何其他结构

2 个解决方案

#1


1  

Your data is in a standard format (JSON) and there are parsers already written for it. You can download Json.NET easy through NuGet in Visual Studio.

您的数据采用标准格式(JSON),并且已经为其编写了解析器。您可以在Visual Studio中通过NuGet轻松下载Json.NET。

Regular expressions are a powerful tool that makes pattern matching a lot simpler. For me that's as far as they go. They can be used to create parsers and all sorts of other things, but it's complicated.

正则表达式是一种强大的工具,可以使模式匹配更加简单。对我而言,就他们而言。它们可以用来创建解析器和各种其他东西,但它很复杂。

So you could create your own JSON parser using regular expressions, but it'll take a lot of time. It would be like building a lockpick when there is a key available.

所以你可以使用正则表达式创建自己的JSON解析器,但这需要花费很多时间。当有钥匙可用时,就像建立一个锁具一样。

#2


0  

JavaScriptSerializer may satisfy your needs

JavaScriptSerializer可以满足您的需求

using System.Web.Script.Serialization;
var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string,string>>(jsonText);
Console.WriteLine(dict["some_number"]);

See: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

#1


1  

Your data is in a standard format (JSON) and there are parsers already written for it. You can download Json.NET easy through NuGet in Visual Studio.

您的数据采用标准格式(JSON),并且已经为其编写了解析器。您可以在Visual Studio中通过NuGet轻松下载Json.NET。

Regular expressions are a powerful tool that makes pattern matching a lot simpler. For me that's as far as they go. They can be used to create parsers and all sorts of other things, but it's complicated.

正则表达式是一种强大的工具,可以使模式匹配更加简单。对我而言,就他们而言。它们可以用来创建解析器和各种其他东西,但它很复杂。

So you could create your own JSON parser using regular expressions, but it'll take a lot of time. It would be like building a lockpick when there is a key available.

所以你可以使用正则表达式创建自己的JSON解析器,但这需要花费很多时间。当有钥匙可用时,就像建立一个锁具一样。

#2


0  

JavaScriptSerializer may satisfy your needs

JavaScriptSerializer可以满足您的需求

using System.Web.Script.Serialization;
var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string,string>>(jsonText);
Console.WriteLine(dict["some_number"]);

See: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx