2016-11-14 91 views
-4

我有以下大字符串,我希望所有的.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}] 

我想文章從刺:

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

謝謝...提前

+0

對象創建一個類更容易和convienent方法是什麼你有沒有嘗試過? –

+0

我曾嘗試從此:http://stackoverflow.com/questions/13780654/extract-all-strings-between-two-strings –

+0

是像json或什麼的格式的字符串? –

回答

1

如果你只需要.JPG文件名,你可以試試這個簡單的正則表達式:

\w+\.jpg 

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

可以按如下方式使用C#中使用它:

var regex = new Regex(@"\w+\.jpg"); 
return regex.Matches(strInput); 

來源:1

+0

是的,我想這個,但我怎麼可以在C#中使用? –

+1

你應該逃脫「。」字符。你的正則表達式也匹配2ff1_jpg,asdasd,jpg –

+0

@RAJNIKPATEL我更新了我的答案。 – Ibrahim

0

你的字符串好像它是一個JSON字符串。因而這將是desealize的JSON到一個對象,並獲取像

價值爲您的JSON字符串

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); 
} 
+0

嘿!憤怒的downvoter。請說明downvote的原因。 –