从c#中的字符串中提取List中的所有.jpg文件名字符串

时间:2016-11-14 04:37:40

标签: c# .net regex

我有跟随大字符串,我希望所有带有扩展名的.jpg文件名到C#中的字符串列表但我不知道如何获取它。

[{\"url\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/dd2d49c78b1fe3a9cea5761d90132ff1.jpg\",\"href\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/dd2d49c78b1fe3a9cea5761d90132ff1.jpg\",\"filename\":\"dd2d49c78b1fe3a9cea5761d90132ff1.jpg\",\"url_master\":\"menus_original\\/799\\/799\\/dd2d49c78b1fe3a9cea5761d90132ff1.jpg\",\"path_master\":\"\\/home\\/foodie\\/zomato_data\\/menus_original\\/799\\/799\\/dd2d49c78b1fe3a9cea5761d90132ff1.jpg\",\"data_center\":\"\",\"menu_type\":\"FOOD\",\"title\":\"FOOD\",\"menu_type_class\":\"FOOD\",\"real_menu_type\":\"FOOD\",\"is_salt_special_menu\":0,\"start_date\":\"\",\"consumer_upload\":0,\"start_date_formatted\":\"\",\"end_date\":\"\",\"end_date_formatted\":\"\",\"id\":131698200},{\"url\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/f9f923c43b6b2d2a87ad8ce22b9995da.jpg\",\"href\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/f9f923c43b6b2d2a87ad8ce22b9995da.jpg\",\"filename\":\"f9f923c43b6b2d2a87ad8ce22b9995da.jpg\",\"url_master\":\"menus_original\\/799\\/799\\/f9f923c43b6b2d2a87ad8ce22b9995da.jpg\",\"path_master\":\"\\/home\\/foodie\\/zomato_data\\/menus_original\\/799\\/799\\/f9f923c43b6b2d2a87ad8ce22b9995da.jpg\",\"data_center\":\"\",\"menu_type\":\"FOOD\",\"title\":\"FOOD\",\"menu_type_class\":\"FOOD\",\"real_menu_type\":\"FOOD\",\"is_salt_special_menu\":0,\"start_date\":\"\",\"consumer_upload\":0,\"start_date_formatted\":\"\",\"end_date\":\"\",\"end_date_formatted\":\"\",\"id\":131698203},{\"url\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/ea3117de65882f14723480841940b5b1.jpg\",\"href\":\"https:\\/\\/\\/data\\/menus\\/799\\/799\\/ea3117de65882f14723480841940b5b1.jpg\",\"filename\":\"ea3117de65882f14723480841940b5b1.jpg\",\"url_master\":\"menus_original\\/799\\/799\\/ea3117de65882f14723480841940b5b1.jpg\",\"path_master\":\"\\/home\\/foodie\\/zomato_data\\/menus_original\\/799\\/799\\/ea3117de65882f14723480841940b5b1.jpg\",\"data_center\":\"\",\"menu_type\":\"FOOD\",\"title\":\"FOOD\",\"menu_type_class\":\"FOOD\",\"real_menu_type\":\"FOOD\",\"is_salt_special_menu\":0,\"start_date\":\"\",\"consumer_upload\":0,\"start_date_formatted\":\"\",\"end_date\":\"\",\"end_date_formatted\":\"\",\"id\":131698204}]

我想从下面列出sting:

dd2d49c78b1fe3a9cea5761d90132ff1.jpg
dd2d49c78b1fe3a9cea5761d90132ff1.jpg
dd2d49c78b1fe3a9cea5761d90132ff1.jpg
f9f923c43b6b2d2a87ad8ce22b9995da.jpg
f9f923c43b6b2d2a87ad8ce22b9995da.jpg

谢谢......提前

2 个答案:

答案 0 :(得分:1)

如果您只需要.jpg文件名,可以试试这个简单的Regex:

float("-inf")

演示:https://regex101.com/r/KMWtZY/1

您可以将它与C#一起使用,如下所示:

\w+\.jpg

来源:1

答案 1 :(得分:0)

你的字符串似乎是一个JSON字符串。因此,将JSON解密为对象并获取像

这样的值会更容易,更方便。

为JSON字符串的Object创建一个类

public class YourData
{
    public string url { get; set; }
    public string href { get; set; }
    public string filename { get; set; }
    public string url_master { get; set; }
    public string path_master { get; set; }
    public string data_center { get; set; }
    public string menu_type { get; set; }
    public string title { get; set; }
    public string menu_type_class { get; set; }
    public string real_menu_type { get; set; }
    public int is_salt_special_menu { get; set; }
    public string start_date { get; set; }
    public int consumer_upload { get; set; }
    public string start_date_formatted { get; set; }
    public string end_date { get; set; }
    public string end_date_formatted { get; set; }
    public int id { get; set; }
}

//From wherever you are reading it.
string jsonstr = "Your Json String";
//I Removed all the / and \ from the string
jsonstr = jsonstr.Replace("/", "");
jsonstr = jsonstr.Replace("\\", "");
//at last of the string you have something like this 
//;\n        .menuTypes = [\"DEFAULT\",\"FOOD\",\"BAR\",\"DELIVERY\",\"SPECIAL\",\"TAKEAWAY\",\"INTERNAL\"];\n
//which is not the part of the JSON string. So I removed that part as well to make it a valid JSON
jsonstr = jsonstr.Remove(jsonstr.IndexOf(";n"));
//Console.WriteLine(jsonstr); //You can uncomment it to see how JSON looks after cleaning.
//Just Deserialize the JSON
List<YourData> yd = JsonConvert.DeserializeObject<List<YourData>>(jsonstr);
//Loop to get all the filenames or any other fields you want 
foreach(YourData ydd in yd)
{
    Console.WriteLine(ydd.filename);
}