C# 使用正则表达式和C解析字符串#
我有下面的正则表达式-C# 使用正则表达式和C解析字符串#,c#,regex,parsing,C#,Regex,Parsing,我有下面的正则表达式- string s = "{\"data\": {\"words\": [{\"wordsText\": \"Three Elephants /d in the jungle\"}]}}"; string[] words = s.Split('\\',':','[',']','{','}','"'); foreach (string word in words) { Console.WriteLine(word); }
string s = "{\"data\": {\"words\": [{\"wordsText\": \"Three Elephants /d
in the jungle\"}]}}";
string[] words = s.Split('\\',':','[',']','{','}','"');
foreach (string word in words)
{
Console.WriteLine(word);
}
哪个输出-
data
words
wordsText
Three Elephants /d in the jungle
去除输出中的前3行以便只得到最后一行的最佳方法是什么?丛林中的三头大象/d
我相信如果我在“wordsText\”之后写出所有文本:这可能是一种可行的方法,非常感谢您的帮助。您可以使用RegEx-sure,但因为它看起来像JSON,所以最好使用JSON.NET来解析它
JObject o = JObject.Parse(@"{
""Stores"": [
""Lambton Quay"",
""Willis Street""
],
""Manufacturers"": [
{
""Name"": ""Acme Co"",
""Products"": [
{
""Name"": ""Anvil"",
""Price"": 50
}
]
},
{
""Name"": ""Contoso"",
""Products"": [
{
""Name"": ""Elbow Grease"",
""Price"": 99.95
},
{
""Name"": ""Headlight Fluid"",
""Price"": 4
}
]
}
]
}");
string name = (string)o.SelectToken("Manufacturers[0].Name");
// Acme Co
decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
// 50
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
// Elbow Grease
见:
这使用了.您可以使用RegEx-sure,但是因为它看起来像JSON,所以最好使用JSON.NET来解析它
JObject o = JObject.Parse(@"{
""Stores"": [
""Lambton Quay"",
""Willis Street""
],
""Manufacturers"": [
{
""Name"": ""Acme Co"",
""Products"": [
{
""Name"": ""Anvil"",
""Price"": 50
}
]
},
{
""Name"": ""Contoso"",
""Products"": [
{
""Name"": ""Elbow Grease"",
""Price"": 99.95
},
{
""Name"": ""Headlight Fluid"",
""Price"": 4
}
]
}
]
}");
string name = (string)o.SelectToken("Manufacturers[0].Name");
// Acme Co
decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
// 50
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
// Elbow Grease
见:
这使用了.我会在C中使用正则表达式,您的表达式如下
MatchCollection matchCtrls = Regex.Matches(pageText, @"Th(.*)e", RegexOptions.Singleline);
MatchCollection matchCtrls = Regex.Matches(pageText, @"": \(.*)\", RegexOptions.Singleline);
如果你不知道文本的具体内容,那么可能是这样的
MatchCollection matchCtrls = Regex.Matches(pageText, @"Th(.*)e", RegexOptions.Singleline);
MatchCollection matchCtrls = Regex.Matches(pageText, @"": \(.*)\", RegexOptions.Singleline);
我会在C中使用正则表达式,你的表达式是这样的
MatchCollection matchCtrls = Regex.Matches(pageText, @"Th(.*)e", RegexOptions.Singleline);
MatchCollection matchCtrls = Regex.Matches(pageText, @"": \(.*)\", RegexOptions.Singleline);
如果你不知道文本的具体内容,那么可能是这样的
MatchCollection matchCtrls = Regex.Matches(pageText, @"Th(.*)e", RegexOptions.Singleline);
MatchCollection matchCtrls = Regex.Matches(pageText, @"": \(.*)\", RegexOptions.Singleline);
您是对的,这是JSON,但是在.NET>中使用的是什么库,因为它无法识别JObject,我将标记为正确。@使用JSON.NET库的Jambo。我用一个指向它的链接更新了这个问题。你是对的,这是JSON,但是在.NET>中使用了什么库,因为它无法识别JObject,我将标记为正确的。@使用JSON.NET库的Jambo。我用一个链接更新了这个问题。using System.Text.RegularExpressions;使用System.Text.RegularExpressions;