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.

Anh Nguyen
  • 313
  • 5
  • 11
  • 22
  • 1
    Can you tell us what you have done already? Have you gone to regex tutorials. there are a ton online – Justin Pihony Dec 12 '13 at 16:11
  • 4
    You need to use a JSON parser. – SLaks Dec 12 '13 at 16:12
  • As @LSaks says, you've got a set of [json](http://json.org/) data. Questions like [this one](http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c) 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. – brandonscript Dec 12 '13 at 16:13
  • i tried function split in c#, it worked but i think it's not the good solution for it. – Anh Nguyen Dec 12 '13 at 16:14
  • Parse that string in an JSON Object. After that, your job is pretty easy. – everton Dec 12 '13 at 16:15
  • Although the data looks like JSON, why does OP need a JSON parser for this? Couldn't he do it with just `Regex`? – Shiva Dec 12 '13 at 16:15
  • @Shiva *really*? http://cstheory.stackexchange.com/questions/3987/is-json-a-regular-language yes you CAN do it with regex, but it's not a good idea, and it's going to process slower than actually parsing it with a parser. – brandonscript Dec 12 '13 at 16:16
  • Anyone can show us an example with json? – Sangram Nandkhile Dec 12 '13 at 16:17
  • 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. – Gusdor Dec 12 '13 at 16:18
  • Thanks @r3mus Not sure why you linked to that `cstheory` question and also threw in some attitude. My question was a sincere one. More importantly, the answer with most upvotes is a `Regex` answer ;) – Shiva Dec 12 '13 at 16:19
  • @Shiva sorry, just that point is argued on SO almost every day, and after answering the same question on a daily basis, it gets a little dry after awhile ;) It wasn't intended to be mean, more an expression of flabbergast. – brandonscript Dec 12 '13 at 16:22
  • @r3mus Ok. Why would you even be flabbergasted? I never claimed or implied that `JSON` was a regular language. I mean, the expansion of `JSON` should be clear enough to even a non-programmer that 'JavaScript Object Notation` mean a data format, not a language construct. – Shiva Dec 12 '13 at 16:28
  • @Shiva fair enough. It's early in the morning for me and I'm cantankerous ;) – brandonscript Dec 12 '13 at 16:31

1 Answers1

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

falsetru
  • 357,413
  • 63
  • 732
  • 636