1

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.

13
  • 1
    Can you tell us what you have done already? Have you gone to regex tutorials. there are a ton online Commented Dec 12, 2013 at 16:11
  • 4
    You need to use a JSON parser. Commented Dec 12, 2013 at 16:12
  • As @LSaks says, you've got a set of json data. Questions like this one will get you started. Once you've parsed the json, you can then easily use a regular expression to capture what's between the decoded audio\16\Phai... etc. Commented Dec 12, 2013 at 16:13
  • i tried function split in c#, it worked but i think it's not the good solution for it. Commented Dec 12, 2013 at 16:14
  • 1
    @r3mus I dispute the claim that a regex engine that creates several result objects is slower that a JSON parser that creates a whole tree. Citation required, sir. Commented Dec 12, 2013 at 16:18

1 Answer 1

6

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]);

ideone

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.