1

I would like to parse the following bibtex string or maybe a whole .bib file using php or javascript.

Any suggestions?

The string looks like:

@article{Sng:2010:PMW:1750585.1750678,
 author = {Sng, Colin T. S. and Manlove, David F.},
 title = {Popular matchings in the weighted capacitated house allocation problem},
 journal = {J. of Discrete Algorithms},
 issue_date = {June, 2010},
 volume = {8},
 number = {2},
 month = jun,
 year = {2010},
 issn = {1570-8667},
 pages = {102--116},
 numpages = {15},
 url = {http://dx.doi.org/10.1016/j.jda.2008.11.008},
 doi = {10.1016/j.jda.2008.11.008},
 acmid = {1750678},
 publisher = {Elsevier Science Publishers B. V.},
 address = {Amsterdam, The Netherlands, The Netherlands},
 keywords = {Maximum popular matching, Polynomial-time algorithm, Popular matching     problem, Priorities, Strict preference lists},
} 
hakre
  • 193,403
  • 52
  • 435
  • 836
glarkou
  • 7,023
  • 12
  • 68
  • 118

1 Answers1

6

I can't quite remember the BibTeX syntax, but I'd suggest to convert the syntax to make it parsable by JSON.parse, like this:

var bibjson = bibtex.replace(/(\w+)\s*=\s*\{/g,"\"$1\": \"")
          .replace(/\}(?=\s*[,\}])/g,"\"")
          .replace(/@(\w+)\s*\{([^,]*)/,"{\"$1\": \"$2\"");
var bibobj = JSON.parse(bibjson);
MaxArt
  • 22,200
  • 10
  • 82
  • 81
  • Thanks for your time. I believe there is an error in your code. I cannot run it. There is an error in the first line. `Parse error: syntax error, unexpected '/', expecting ')' in /homepages/26/d94605010/htdocs/lz/php/index.php(153) : eval()'d code on line 21` – glarkou May 26 '12 at 18:57
  • I gave you a Javascript solution, not a PHP one. – MaxArt May 26 '12 at 19:02
  • Is it possible to put it on jsfiddle? – glarkou May 26 '12 at 19:09
  • 1
    Sure: http://jsfiddle.net/hFKjX/ I made some little modification to you BibTeX source, though. Namely, i put "jun" between braces, and deleted the last comma before the closing brace. If you think your BibTeX source will have values not embraced by curly braces, the code has to be modified. – MaxArt May 26 '12 at 19:27
  • I'd like to use your solution in a program I'm writing and I want to attribute all code I didn't write to its original author. Did you create this code yourself? It's so elegant. – Phlox Midas Jun 21 '12 at 15:02
  • 1
    Sure, I did this on my own. Feel free to copy, edit and share it, and if you want to cite me as the author, well... I'll surely don't mind :) – MaxArt Jun 21 '12 at 20:46
  • @tobibeer It *does* work, but limited to salamis' question. For other test cases, the code should be adapted. – MaxArt Jan 26 '15 at 23:04