Reputation: 313
I have a string in c# like this :
{ name: "Phai dấu cuộc tình", mp3: "audio\\16\\Phai dau cuoc tinh.mp3"},{ name: "Caravan of life", mp3: "audio\\4\\Caravan of life.mp3"},{ name: "I'm Forbidden", mp3: "audio\\11\\I'm Forbidden.mp3"},{ name: "Cause i love you", mp3: "audio\\6\\Cause i love you.mp3"},{ name: "Chỉ là giấc mơ", mp3: "audio\\8\\Chi la giac mo.mp3"},{ name: "Lột xác", mp3: "audio\\12\\Lot xac.mp3"}
I want to get the number between "\\" to a new string. For example, the result will be : 16;4;11;6;8;12. Any help would be great.
Upvotes: 1
Views: 146
Reputation: 369494
Using positive lookaround assertions:
string str = "{ name: \"Phai dấu cuộc tình\", mp3: \"audio\\16\\Phai dau cuoc tinh.mp3\"},{ name: \"Caravan of life\", mp3: \"audio\\4\\Caravan of life.mp3\"},{ name: \"I'm Forbidden\", mp3: \"audio\\11\\I'm Forbidden.mp3\"},{ name: \"Cause i love you\", mp3: \"audio\\6\\Cause i love you.mp3\"},{ name: \"Chỉ là giấc mơ\", mp3: \"audio\\8\\Chi la giac mo.mp3\"},{ name: \"Lột xác\", mp3: \"audio\\12\\Lot xac.mp3\"}";
foreach (var match in Regex.Matches(str, @"(?<=\\)\d+(?=\\)"))
Console.WriteLine(match);
Alternative: capturing group
foreach (Match match in Regex.Matches(str, @"\\(\d+)\\"))
Console.WriteLine(match.Groups[1]);
Upvotes: 6