import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^@(.*?)\\{(.*?),((.|\\n)*?)+(\\}\\}|\\n\\})+$";
final String string = "@inproceedings{martens2010deep,\n"
+ "title={Deep learning via Hessian-free optimization},\n"
+ "author={Martens, James},\n"
+ "booktitle={Proceedings of the 27th International Conference on Machine Learning (ICML-10)},\n"
+ "pages={735--742},\n"
+ "year={2010}\n"
+ "}\n"
+ "@article-journal{AAA,\n"
+ " url = {http://www.example.com},\n"
+ " year = {2016},\n"
+ " month = {},\n"
+ " publisher = {Elsevier},\n"
+ " journal = {{AAA Journal}},\n"
+ " author = {Austin Anderson and Austin Arnold},\n"
+ " title = {{Nothing To See Here}},\n"
+ "}\n"
+ "@article-journal{ZZZ,\n"
+ " url = {http://www.example.com},\n"
+ " year = {2016},\n"
+ " month = {},\n"
+ " publisher = {Elsevier},\n"
+ " journal = {{ZZZ Journal}},\n"
+ " author = {Austin Zyzygy},\n"
+ " title = {{Moving On Up}},\n"
+ "}\n"
+ "@article-journal{InstMed2001,\n"
+ " url = {http://www.ncbi.nlm.nih.gov/pubmed/25057539},\n"
+ " year = {2001},\n"
+ " month = {},\n"
+ " publisher = {{National Academy of Sciences}},\n"
+ " doi = {10.17226/10027},\n"
+ " volume = {},\n"
+ " number = {},\n"
+ " pages = {},\n"
+ " pmid = {25057539},\n"
+ " pmcid = {},\n"
+ " journal = {{}},\n"
+ " author = {{National Academy of Sciences}},\n"
+ " title = {{Crossing the Quality Chasm: A New Health System for the 21st Century}},\n"
+ "}\n"
+ "@article-journal{Wallace2013,\n"
+ " url = {http://apt.rcpsych.org/content/19/4/250},\n"
+ " year = {2013},\n"
+ " month = {July},\n"
+ " publisher = {{Royal College of Psychiatrists}},\n"
+ " doi = {10.1192/apt.bp.112.010389},\n"
+ " volume = {19},\n"
+ " number = {4},\n"
+ " pages = {250-258},\n"
+ " journal = {{BJPsych Advances}},\n"
+ " author = {John Wallace},\n"
+ " title = {{Lost in translation: transferring knowledge from research to clinical practice}},\n"
+ "}\n"
+ "@article-journal{Glasziou2005,\n"
+ " url = {http://ebn.bmj.com/content/8/2/36.full},\n"
+ " year = {2005},\n"
+ " month = {},\n"
+ " publisher = {{British Medical Journal}},\n"
+ " doi = {10.1136/ebn.8.2.36},\n"
+ " volume = {8},\n"
+ " number = {},\n"
+ " pages = {36-38},\n"
+ " journal = {{Evidence Based Nursing}},\n"
+ " author = {Paul Glasziou and Brian Haynes},\n"
+ " title = {{The paths from research to improved health outcomes}},\n"
+ "}\n"
+ "@article-journal{Glasziou2011,\n"
+ " url = {http://pmj.bmj.com/content/early/2011/06/29/pgmj.2010.116012.full.html},\n"
+ " year = {2011},\n"
+ " month = {June},\n"
+ " publisher = {{British Medical Journal}},\n"
+ " doi = {10.1136/pgmj.2010.116012},\n"
+ " volume = {},\n"
+ " number = {},\n"
+ " pages = {},\n"
+ " pmid = {},\n"
+ " pmcid = {},\n"
+ " journal = {{Postgraduate Medicine Journal}},\n"
+ " author = {Sharon Mickan and Amanda Burls and Paul Glasziou},\n"
+ " title = {{Patterns of 'leakage' in the utilisation of clinical guidelines: a systematic review}},\n"
+ "}\n";
final String subst = "{\\\"id\\\":\\\"${2}\\\",\\\"type\\\":${1},\\\"content\\\":[{${3}]}}";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html