1
"artistName":"Travie McCoy", "collectionName":"Billionaire (feat. Bruno Mars) - Single", "trackName":"Billionaire (feat. Bruno Mars)",

i wish to get the artist name so Travie McCoy from within that code using regex, please not i am using regexkitlite for the iphone sdk if this changes things.

Thanks

4
  • I'm sure there is a parser for that (like, this one for example?). You do not want to do this with regex. Commented Sep 29, 2010 at 20:05
  • 1
    i'd prefer regex to be honest, im trying to learn it Commented Sep 29, 2010 at 20:06
  • 3
    @user: This may be so, but this is a genuinely bad case for regex. Whenever nested structures (and yes, CSV or JSON are nested structures) need parsing, use a parser. Regex can't cope with nested structures. You might get close, but you never cover 100%. -- Inevitably, someone will come and provide a regex-based answer that works. Except in the corner cases. That will of course never happen. Except when they do. Use at your own peril. ;-) Commented Sep 29, 2010 at 20:30
  • WHat do you mean by "two strings", it looks like one long comma separated string containing quotes and characters. Confirm that it's all in one string and a regex can be made. Commented Sep 29, 2010 at 22:47

2 Answers 2

2

"?artistName"?\s*:\s*"([^"]*)("|$) should do the trick. It even handles some variations in the string:

  • White space before and after the :
  • artistName with and without the quotes
  • missing " at the end of the artist name if it is the last thing on the line

But there will be many more variations in the input you might encounter that this regex will not match.

Also you don’t want to use a regex for matching this for performance reasons. Right now you might only be interested in the artistName field. But some time later you will want information from the other fields. If you just change the field name in the regex you’ll have to match the whole string again. Much better to use a parser and transform the whole string into a dictionary where you can access the different fields easily. Parsing the whole string shouldn’t take much longer than matching the last key/value pair using a regex.

This looks like some kind of JSON, there are lots of good and complete parsers available. It isn’t hard to write one yourself though. You could write a simple recursive descent parser in a couple of hours. I think this is something every programmer should have done at least once.

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

2 Comments

Thanks for this it kinda worked i got, and yes i wrote my own xml parser. but i just couldnt be arsed with this
Awesome man thanks :), and yes i have written my own parser, i did a xml one i bloody hate the nsxmlparser :)
0
\"?artistName\"?\s*:\s*\"([^\"]*)(\"|$)

Thats for objective c

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.