I'm trying to create a regular expression to capture in-text citations.
Here's a few example sentences of in-text citations:
... and the reported results in (Nivre et al., 2007) were not representative ...
... two systems used a Markov chain approach (Sagae and Tsujii 2007).
Nivre (2007) showed that ...
... for attaching and labeling dependencies (Chen et al., 2007; Dredze et al., 2007).
Currently, the regular expression I have is
\(\D*\d\d\d\d\)
Which matches examples 1-3, but not example 4. How can I modify this to capture example 4?
Thanks!
Building on Tex's answer, I've written a very simple Python script called Overcite to do this for a friend (end of semester, lazy referencing you know how it is). It's open source and MIT licensed on Bitbucket.
It covers a few more cases than Tex's which might be helpful (see the test file), including ampersands and references with page numbers. The whole script is basically:
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)
\((.+?)\)
should capture all of them
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With