我正在尝试创建一个正则表达式来捕获文本引用。
以下是文本引用的几个例句:
...... (Nivre et al。,2007)中报告的结果不具代表性......
......两个系统使用马尔可夫链方法(Sagae和Tsujii 2007)。
Nivre (2007)表明......
- 醇>
...用于附加和标记依赖关系(Chen et al。,2007; Dredze et al。,2007)。
目前,我的正则表达式是
\(\D*\d\d\d\d\)
哪个匹配示例1-3,但不匹配示例4.如何修改此示例以捕获示例4?
谢谢!
答案 0 :(得分:4)
我最近为此目的使用了这样的东西:
#!/usr/bin/env perl
use 5.010;
use utf8;
use strict;
use autodie;
use warnings qw< FATAL all >;
use open qw< :std IO :utf8 >;
my $citation_rx = qr{
\( (?:
\s*
# optional author list
(?:
# has to start capitalized
\p{Uppercase_Letter}
# then have a lower case letter, or maybe an apostrophe
(?= [\p{Lowercase_Letter}\p{Quotation_Mark}] )
# before a run of letters and admissible punctuation
[\p{Alphabetic}\p{Dash_Punctuation}\p{Quotation_Mark}\s,.] +
) ? # hook if and only if you want the authors to be optional!!
# a reasonable year
\b (18|19|20) \d\d
# citation series suffix, up to a six-parter
[a-f] ? \b
# trailing semicolon to separate multiple citations
; ?
\s*
) +
\)
}x;
while (<DATA>) {
while (/$citation_rx/gp) {
say ${^MATCH};
}
}
__END__
... and the reported results in (Nivré et al., 2007) were not representative ...
... two systems used a Markov chain approach (Sagae and Tsujii 2007).
Nivre (2007) showed that ...
... for attaching and labelling dependencies (Chen et al., 2007; Dredze et al., 2007).
运行时,它会产生:
(Nivré et al., 2007)
(Sagae and Tsujii 2007)
(2007)
(Chen et al., 2007; Dredze et al., 2007)
答案 1 :(得分:3)
在Tex's answer的基础上,我编写了一个非常简单的Python脚本,名为Overcite,为朋友做这个(学期结束,懒惰引用,你知道它是怎么回事)。它是开源的,MIT在Bitbucket上获得许可。
它涵盖了几个可能有用的案例(参见测试文件),包括符号和带页码的引用。整个脚本基本上是:
author = "(?:[A-Z][A-Za-z'`-]+)"
etal = "(?:et al.?)"
additional = "(?:,? (?:(?:and |& )?" + author + "|" + etal + "))"
year_num = "(?:19|20)[0-9][0-9]"
page_num = "(?:, p.? [0-9]+)?" # Always optional
year = "(?:, *"+year_num+page_num+"| *\("+year_num+page_num+"\))"
regex = "(" + author + additional+"*" + year + ")"
matches = re.findall(regex, text)
答案 2 :(得分:2)
/\(\D*\d\d\d\d(?:;\D*\d\d\d\d)*\)/
答案 3 :(得分:2)
\((.+?)\)
应该捕获所有这些
答案 4 :(得分:1)
您所需要的只是插入与您的模式的零次或多次匹配的模式以用于引用,前面加分号。从概念上讲,它是:\(cite(; cite)*\)
。
模式为:\(\D*\d{4}(;\D*\d{4})*\)
。
答案 5 :(得分:1)
这是我的解决方案,在C ++中使用boost regex。希望它可以帮助某人: - )
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string_regex.hpp>
#include <boost/regex.h>
using namespace std;
using namespace boost;
int Section::countCitations() {
string Personsname = "([A-Z][a-z'`-]+)"; // Apostrophes like in "D'Alembert" and hyphens like in "Flycht-Eriksson".
string YearPattern = "(, *(19|20)[0-9][0-9]| ?\( *(19|20)[0-9][0-9]\))"; // Either Descartes, 1990 or Descartes (1990) are accepted.
string etal = "( et al.?)"; // You may find this
string andconj = Personsname + " and " + Personsname;
string commaconj = Personsname + ", " + "(" + Personsname + "|"+"et al.?"+")"; // Some authors write citations like "A, B, et al. (1995)". The comma before the "et al" pattern is not rare.
string totcit = Personsname+"?"+etal+"?"+"("+andconj+"|"+commaconj+")*"+etal+"?"+YearPattern;
// Matches the following cases:
// Xig et al. (2004);
// D'Alembert, Rutherford et al (2008);
// Gino, Nino and Rino, Pino (2007)
// (2009)
// Gino, et al. (2005)
cout << totcit << endl;
regex citationform(totcit);
int count = 0;
string_range citation;
string running_text(text.begin(), text.end() );
while ((citation = find_regex(running_text, citationform)) ) { // Getting the last one
++count;
string temp(running_text.begin(), citation.end() );
running_text = running_text.substr( temp.length()-1 );
}
return count;
}