首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在多个正则表达式之间进行匹配和替换

如何在多个正则表达式之间进行匹配和替换
EN

Stack Overflow用户
提问于 2015-12-23 16:32:18
回答 1查看 86关注 0票数 2

在用户提供的任何科学论文中,程序必须.

  1. 查找:

1.a -数字引文。

这些方法不是从总结的文本中复制完整的句子,而是压缩句子1、4-6,或者从头开始重新生成3中的新句子。

1.b -论文末尾的APA引用&只是作者的名字+年份。

  1. 将数字转换为APA。

这些方法不是从总结的文本中复制完整的句子,而是压缩句子(Jing 2000;Knight和Marcu 2000;Sporleder和Lapata 2005;Steinberger和Ježek 2006),或者从零开始重新生成新的句子(McKeown等人,1999年)。

我认为准则是:

代码语言:javascript
运行
复制
"\[(\d.*?)\]"

用于数字引文。

代码语言:javascript
运行
复制
"\d+([\.]([\ ])(([\D*])*([\,]))*([\ ][\w][\.]))|[\d]{4}"

APA的引文风格。

我的问题是如何取代第一个模式中的第二个模式?

EN

回答 1

Stack Overflow用户

发布于 2015-12-23 19:26:29

使用String.prototype.replace()与回调函数一起使用,该函数将数字字符串(以逗号分隔,可能是一个范围)拆分为一个数字数组。然后循环遍历数字,并使用其他正则表达式查找作者/年份。然后加入这些字符串并返回它以进行替换。

代码语言:javascript
运行
复制
var string = `Instead of reproducing full sentences from the summarized text, these methods either compress the sentences [1, 4-6], or re-generate new sentences from scratch [3].
1. Jing, H.: Sentence Reduction for Automatic Text Summarization. In Proceedings of the 6th Applied Natural Language Processing Conference, Seattle, USA, 2000, pp. 310–315.
3. McKeown et al: Sentence Reduction for Automatic Text Summarization. In Proceedings of the 6th Applied Natural Language Processing Conference, Seattle, USA, 1999, pp. 310–315.
4. Knight and Marcu.: Sentence Reduction for Automatic Text Summarization. In Proceedings of the 6th Applied Natural Language Processing Conference, Seattle, USA, 2000, pp. 310–315.
5. Sporleder and Lapata: Sentence Reduction for Automatic Text Summarization. In Proceedings of the 6th Applied Natural Language Processing Conference, Seattle, USA, 2005, pp. 310–315.
6. Steinberger and Ježek: Sentence Reduction for Automatic Text Summarization. In Proceedings of the 6th Applied Natural Language Processing Conference, Seattle, USA, 2006, pp. 310–315.`;

string.replace(/\[(\d[^\]]*)\]/g, function(matches, reference) {
    var numbers = [],
        authors = [];

    reference.split(/,\s*/g).forEach(function(number) {
        if(number.indexOf('-') !== -1) {
            var range = number.split('-');
            for(var i = range[0]; i <= range[1]; i++) {
                numbers.push(i);
            }
        } else numbers.push(number);
    });

    numbers.forEach(function(number) {
        var regex = new RegExp(number + '\\. ([^:]+):.*?(\\d{4})'),
            matches = regex.exec(string);
        authors.push(matches[1] + ' ' + matches[2]);
    });

    return '(' + authors.join('; ') + ')';
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34440010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档