Latex 使用Raku从.bib文件中提取(以前称为Perl 6)
我在用LaTeX撰写论文时,有此参考管理:Latex 使用Raku从.bib文件中提取(以前称为Perl 6),latex,raku,tex,bibtex,Latex,Raku,Tex,Bibtex,我在用LaTeX撰写论文时,有此参考管理: @article{garg2017patch, title={Patch testing in patients with suspected cosmetic dermatitis: A retrospective study}, author={Garg, Taru and Agarwal, Soumya and Chander, Ram and Singh, Aashim and Yadav, Pravesh}, journal={J
@article{garg2017patch,
title={Patch testing in patients with suspected cosmetic dermatitis: A retrospective study},
author={Garg, Taru and Agarwal, Soumya and Chander, Ram and Singh, Aashim and Yadav, Pravesh},
journal={Journal of Cosmetic Dermatology},
year={2017},
publisher={Wiley Online Library}
}
@article{hauso2008neuroendocrine,
title={Neuroendocrine tumor epidemiology},
author={Hauso, Oyvind and Gustafsson, Bjorn I and Kidd, Mark and Waldum, Helge L and Drozdov, Ignat and Chan, Anthony KC and Modlin, Irvin M},
journal={Cancer},
volume={113},
number={10},
pages={2655--2664},
year={2008},
publisher={Wiley Online Library}
}
@article{siperstein1997laparoscopic,
title={Laparoscopic thermal ablation of hepatic neuroendocrine tumor metastases},
author={Siperstein, Allan E and Rogers, Stanley J and Hansen, Paul D and Gitomirsky, Alexis},
journal={Surgery},
volume={122},
number={6},
pages={1147--1155},
year={1997},
publisher={Elsevier}
}
如果有人想知道什么是bib文件,你可以找到它的详细信息
我想用Perl 6对此进行解析,以提取密钥和标题,如下所示:
garg2017patch: Patch testing in patients with suspected cosmetic dermatitis: A retrospective study
hauso2008neuroendocrine: Neuroendocrine tumor epidemiology
siperstein1997laparoscopic: Laparoscopic thermal ablation of hepatic neuroendocrine tumor metastases
你能帮我做到这一点吗,可能有两种方式:
- 一个完整而详细的答案,完全符合@Suman的要求
- 对“我想解析X,有人能帮忙吗?”的一般性回答
>raku-e'表示slurp()~~m:g/“@article\{”(+)\,\s+
“title=\{”(+)\}/->$/{put“$0:$1\n”}”
这将精确地生成您指定的输出:
garg2017patch:疑似化妆品皮炎患者的斑贴试验:一项回顾性研究
Hauso2008神经内分泌:神经内分泌肿瘤流行病学
Siperstein1997腹腔镜:腹腔镜热消融治疗肝神经内分泌肿瘤转移
同一条语句,但在脚本中
跳过shell转义并添加:
- 空白
- 评论
对于slurp()#“slurp”(读取全部)标准输入,然后
~m:global#将其“全局”(所有匹配项)与
/“@article{'(+)”,“\s+#一个使用(`(…)`)来
'title={'(+)'}'/#捕获文章id和标题,然后
->$/{put“$0:$1\n”}#对于每篇文章,打印“文章id:标题”。
如果上面的内容看起来仍然像纯粹的gobbledygook,请不要担心。后面的章节将解释上述内容,同时还将介绍更通用、更清晰、更可读的代码。[2]
四条语句而不是一条
my\input=slurp;
my\pattern=规则{'@article{'(+)','
'title={'(+)}
my\articles=input.match:pattern,:global;
对于文章->$/{put“$0:$1\n”}
my
声明一个词法变量。Raku支持在变量名的开头。但它也允许开发人员像我一样“删除它们”
my\pattern…
my\pattern=规则{'@article{'(+)','
'title={'(+)}
我已将模式语法从原始一行中的/…/
切换到规则{…}
。我这样做是为了:
- 指定模式而不是匹配的模式
如果我编写了
,则会立即调用匹配来匹配当前主题,并将匹配结果分配给my\pattern=/…/
.Oopspattern
- 消除病理性回溯的风险
经典正则表达式有风险。如果你可以杀死一个疯狂的程序,那没关系,但点击链接阅读它会变得多么糟糕!如果你有文本要与我们共享,请以文本形式发布。发布文本图像毫无意义,毫无帮助,也很粗鲁。@DaveCross:你完全正确,我希望你的消息能被理解!我清理了任务不管怎么说,这都是为了更大的利益…特别是因为它有这么好的答案。谢谢@raiph的清晰解释。现在我能够稍微解释一下Perl 6语法。非常好!但是…值可以是裸数字(年份=2019),用引号括起来(author=“Thor,a.U.”),并且可以(通常)包含大括号中的片段(title={Na{\“\i}ve{S}et{T}heory}…@vonbrand我在这个答案中的目标是解决@SumanKhanal的问题,并概述从正则表达式到完整解析器的一系列解决方案,而不是编写完整的bib解析器。¨扩展这个答案中的解决方案以合并您列出的变体,或者实际上是完整的bib解析规则集,这将是非常直接的。Raku正则表达式/解析是图灵完成,所以他们可以解析任何可以解析的东西。考虑这个答案中的RuKu代码。数字,引号,嵌套括号-作品。Rakudo使用Ruk-RexExs/语法分析该代码。这不是火箭科学,尤其是在使用Ruku时。