- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用正则表达式用 Javascript 解析一个 Bibtex 文件,但我似乎找不到合适的解决方案。在下面的示例中,bj
是一个包含引用书目项的子项的数组。我不得不编写一个很长的正则表达式来考虑值可以分成多行、缺少大括号 ({}
) 或末尾有语法错误逗号 (例如,最后一个字段不应以逗号结尾,但一些 TeX 编辑器不会对此提示)。
这是我用来测试正则表达式的内容:
@inproceedings{Carrel2005,
title = {{Algorithm} for near-optimal autonomous resource management},
author = {Carrel, Ândrew and Palmer, Phil},
notes = nonote ,
booktitle = {8th International Symposium on Artificial {Intelligence,
Robotics}, and Automation in Space},
year = {2005}
blahblah = error,
}
如您所见,一些值被分成两行,并且内部可以有花括号。我一直在努力改进的正则表达式如下:
var txt = "@inproceedings{Carrel2005, \n" +
" title = {{Algorithm} for near-optimal autonomous resource management}, \n" +
" author = {Carrel, Ândrew and Palmer, Phil}, \n" +
" notes = nonote ,\n" +
" booktitle = {8th International Symposium on Artificial Intelligence, \n" +
" Robotics and Automation in Space}, \n" +
" year = {2005} \n" +
" blahblah = error,\n}";
bj = txt.match(/\w*[\t ]*=[\t ]*(\{[\u0020-\u0080\u00A1-\u00FF\u0300-\u036F\t\r\n]*?}|[a-zA-Z0-9]+)[\t ]*(,(?!\s*}))?/g);
解释:
\w* A word for the field name.
[\t ]*=[\t ]* Any number of spaces or tabs after and before the equal sign.
( Start of group 1.
\{ Option 11: starts by an opening curly brace.
[ Start of character class AAA.
unicode-set Letters (basic Latin plus some extensions)
\t\r\n ... or whitespace.
]*? End of character class AAA (with LAZY repetition)
| End of option 11, start of option 12:
[a-zA-Z0-9]+ One or more characters (no underscore or whitespace allowed).
) End of option 12 and group 1.
[\t ]* Any number of tabs or spaces.
( Start of group 2:
, A literal comma
(?!\s*}) ...if it is not followed by whitespace and closing curly braces.
)? End of group 2. ? denotes it is optional.
我无法匹配以多个大括号开头的字段(例如 {{Algorithm} for near...
),也无法正确匹配序列 } 的字段,
在里面找到。
最佳答案
正如我在评论中提到的,匹配任意深度的大括号是不可能的,因为这需要某种状态来存储您看到的数字。你需要一个解析器,然后添加状态。它看起来像:
var txt = "@inproceedings{Carrel2005, \n" +
" title = {{Algorithm} for near-optimal autonomous resource management}, \n" +
" author = {Carrel, Ândrew and Palmer, Phil}, \n" +
" notes = nonote ,\n" +
" booktitle = {8th International Symposium on Artificial Intelligence, \n" +
" Robotics and Automation in Space}, \n" +
" year = {2005} \n" +
" blahblah = error,\n}";
function parseBibTexLine (text) {
var m = text.match(/^\s*(\S+)\s*=\s*/);
if (!m) {
console.log('line: "' + text + '"');
throw new Error('Unrecogonised line format');
}
var name = m[1];
var search = text.slice(m[0].length);
var re = /[\n\r,{}]/g;
var braceCount = 0;
var length = m[0].length;
do {
m = re.exec(search);
if (m[0] === '{') {
braceCount++;
} else if (m[0] === '}') {
if (braceCount === 0) {
throw new Error('Unexpected closing brace: "}"');
}
braceCount--;
}
} while (braceCount > 0);
return {
field:name,
value: search.slice(0, re.lastIndex),
length:length + re.lastIndex + m[0].length
};
}
function parseBibTex (text) {
var m = text.match(/^\s*@([^{]+){([^,\n]+)[,\n]/);
if (!m) {
throw new Error('Unrecogonised header format');
}
var result = {
typeName: m[1].trim(),
citationKey: m[2].trim()
}
text = text.slice(m[0].length).trim();
while (text[0] !== '}') {
var pair = parseBibTexLine(text);
result[pair.field] = pair.value;
text = text.slice(pair.length).trim();
}
return result;
}
console.log(parseBibTex(txt));
我当然没有对此进行过深入测试,但是当根据您的输入运行时,我得到:
{ typeName: 'inproceedings',
citationKey: 'Carrel2005',
title: '{{Algorithm} for near-optimal autonomous resource management}',
author: '{Carrel, Ândrew and Palmer, Phil}',
notes: 'nonote ,',
booktitle: '{8th International Symposium on Artificial Intelligence, \n Robotics and Automation in Space}',
year: '{2005}',
blahblah: 'error,' }
关于javascript - 如何使用 JavaScript 和 RegEx 解析复杂的 BibTex 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34221996/
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
假设我有 10 篇文章的 bibtex 数据。有没有办法在 jabref 中从中创建 10 个条目? 解决方案是打开 .bib 文件并将数据粘贴到其中。 最佳答案 选择构成多个数据条目的 BibTex
假设我有 10 篇文章的 bibtex 数据。有没有办法在 jabref 中从中创建 10 个条目? 解决方案是打开 .bib 文件并将数据粘贴到其中。 最佳答案 选择构成多个数据条目的 BibTex
Jabref 支持以不同格式导出选定的条目,但我想知道它是否真的可以导出为 bibtex 格式。选择一些引用文献来创建一个较小的数据库很有用,例如,仅包含某些主题的引用文献或您在特定论文中引用的引用文
我编辑了一个 BibteX 文件,到目前为止,它显示了我需要的所有信息。 FUNCTION {electronic} { output.bibitem format.btitle "title"
(我不确定这个问题是否属于meta网站,但是我们开始吧) 我想在正在撰写的研究论文的书目中添加stackoverflow,并想知道是否有任何bibTeX代码可以这样做。我已经为gnuplot做过 我在
有没有办法从 Google Scholar 或其他地方批量下载 bibtex 数据? 我尝试了一些 GS Scraper,例如, https://github.com/gimoya/theBioBuc
我正在使用 multibib 在附录中创建文章列表。 %Preamble... %Create new macros for citation in "lit" group \newcites{lit
在我的论文中,我使用了以下 latex 模板。但即使在模板中,库也无法正常工作。 https://github.com/MichaelGrupp/TTT 使用 BibTeX 命令后出现以下消息: Pr
我想将引用书目中的一些字母显示为大写。例如: @misc{libsvm, abstract = {LIBSVM is an implbmentation of Support vector mach
好的,这不是一个正确的编程问题,但也许答案会对其他人有所帮助。我一直在尝试为 gnuplot 找到合适的 BibTeX 条目,以包含在科学出版物中。我只是不想引用任何指南,而是引用实际的软件。 有没有
在我的论文中,我使用了以下 latex 模板。但即使在模板中,库也无法正常工作。 https://github.com/MichaelGrupp/TTT 使用 BibTeX 命令后出现以下消息: Pr
是否有任何 python 包来解析 Bibtex 文件,并使用 html/xhtml 格式输出结果,并具有可自定义的样式? 最好用python,不然PHP用的最多的是什么? 最佳答案 Pybliogr
我在 BibTex 中使用三个字母的缩写来表示月份,但是当我编译我的文档时,月份名称是英文的。 我可以以某种方式更改输出语言吗? 我正在使用 natbib ,但如果需要,请不要介意切换回原版 BibT
我想在包含尖括号的 bibtex 引文中放置一个标题字段: @article{ title= { } } 但是当它编译时我会颠倒'!'和 '?'反而。如何摆脱尖括号? 最佳答案 主要的 TeX
我首选的 bibtex 样式文件通过作者的首字母引用。但是,有各种文本应以不同方式引用(例如,Elements Geometrie Algebrique 应始终引用为 [EGA])。我知道如何在 bi
默认情况下(使用 plain 样式)BibTeX 按字母顺序对引文进行排序。 如何按文档中出现的顺序对引文进行排序? 最佳答案 这个问题有三个很好的答案。 如果您对其格式感到满意,请使用 unsrt
我是 LaTeX 和 BibTeX 的新手,请原谅我的无知。我有以下条目: @Article{Hart, author = {P.E. Hart, N.J. Nilsson, B. Raphael},
我正在尝试解析以下 BibTeX 文件(bibliography.bib): @book{Lee2000a, abstract = {Abstract goes here}, author = {Le
我是一名优秀的程序员,十分优秀!