【问题标题】:Get Specific String from huge text in C# [duplicate]从 C# 中的巨大文本中获取特定字符串 [重复]
【发布时间】:2014-02-10 23:38:01
【问题描述】:

我有以下大文本:http://freetexthost.com/15nbm0dhob 而且我需要从standard_resolution中获取所有图片的URL。

"standard_resolution": {
"url": "http://distilleryimage3.s3.amazonaws.com/59d6984092a211e392db12e25f465f4f_8.jpg",
"width": 640,
"height": 640
}

例如:从这里,我想得到:http://distilleryimage3.s3.amazonaws.com/59d6984092a211e392db12e25f465f4f_8.jpg

毕竟我想要一个包含所有标准 URL 的字符串列表。 我正在制作一个 C# 应用程序。

【问题讨论】:

  • 您的文本示例看起来像 JSON。只解析 json 并获取所有“url”值可能会更容易。
  • 这是一个 JSON,但我如何使用 C# 来完成它
  • 同一个问题有更好的答案。 stackoverflow.com/a/21693049/932418

标签: c# regex string image url


【解决方案1】:

Selman22:你的答案会得到所有的 URL,而他只想要 standard_resolution 的 URL。

这是我整理的一个快速而肮脏的正则表达式。 您可能需要稍微调整一下,以涵盖我尚未想到的关于 JSON 结构的所有潜在极端情况,以防它返回的结果与您发布的源略有不同。

const string input = @"
  ""standard_resolution"": {
  ""url"": ""http://distilleryimage3.s3.amazonaws.com/59d6984092a211e392db12e25f465f4f_8.jpg"",
  ""width"": 640,
  ""height"": 640
  }";

var pattern = @"\""standard_resolution\"".*?\""url\""\:\s\""(?<url>.*?)\""";

var urls = Regex.Matches(input.Replace("\r\n", string.Empty), pattern)
    .Cast<Match>()
    .Select(each => each.Groups["url"].Value);

var count = urls.Count();

您的问题直接范围之外的另一种选择是使用 JSON 解析器: Parsing JSON using Json.net

【讨论】:

  • 它只有在它是一个 const 字符串时才有效,但我正在使用这个获取 de JSON:var json = cliente.DownloadString("api.instagram.com/v1/tags/thenight2/media/recent?access_token=**TOKEN**");
  • 您是否像我的代码一样删除输入中的新行? input.Replace("\r\n", string.Empty) 您可能还需要尝试仅删除 \r 或仅删除 \n,具体取决于来自网络服务器的格式。
  • 哦,我明白了。什么是这样的代码模式:“standard_resolution”:{“url”:“http:\/\/distilleryimage9.s3.amazonaws.com\/382b566491f211e3ae050a2150c32a45_8.jpg”,“width”:640,“height”: 640}},"users_in_photo":[],"caption":{"created_time":"1391995536","text":"#thenight2","from":{"username":"thenight2","profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/anonymousUser.jpg","id":"1082107741","full_name":"The Night Party 2"},"id":"652428307931519401 "},"user_has_liked":false,"id":"652428307235264996_1082107741","user":{"username":"thenight2"
  • 代码为一行格式。
【解决方案2】:

我认为你可以使用这种模式:^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$

这是一个例子:

var pattern = @"^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$";
var result= File.ReadAllText("filepath")
            .Split(new[] {'"'}, StringSplitOptions.RemoveEmptyEntries)
            .Where(line => Regex.IsMatch(line, pattern))
            .ToList();

我已经测试过,result 包含25 url 供您输入。

【讨论】:

  • 没错!但我只想要“standard_resolution”网址的
【解决方案3】:

试试:

List<String> urls = new List<String>();                                                            
string txt = "standard_resolution...."; // Your main text                                                                   
while(txt.Contains("url"))                                                                          
{                                                                                                   
    txt = txt.Substring(txt.IndexOf("url\": \""));                                                  
    string geturl = txt.Substring(txt.IndexOf("url")+7, txt.IndexOf(".jpg") - txt.IndexOf("url")-3);
    urls.Add(geturl);                                                                              
    txt = txt.Substring(txt.IndexOf(".jpg"));                                                       
}                                                                                                   

【讨论】:

    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 2015-01-02
    • 2019-02-22
    • 2013-11-13
    • 1970-01-01
    • 2011-10-06
    • 2022-09-23
    • 1970-01-01
    相关资源
    最近更新 更多