求可提取文本中作者-年份引用的正则表达式解决方案
Got it, let's fix that regex for your author-year citation extraction. Here's a refined pattern that addresses all your requirements—including matching both comma-free and comma-separated multi-author cases, supporting international characters, and handling all the edge cases you listed:
\(\K[\p{Lu}][\p{Lu}\p{Ll}\-'\/ ]*(?:(?:\/| & )[\p{Lu}][\p{Lu}\p{Ll}\-'\/ ]*)*(?:(?:,? )(?:et\.? al\.?|u\. ?a\.))?(?:,? )\d{4}(?=\))
Breakdown of the Pattern
Let's walk through each component to understand how it solves your issues:
\(\K: Skips the opening parenthesis so we only capture the content inside (you already knew this, but it's retained for your workflow)[\p{Lu}][\p{Lu}\p{Ll}\-'\/ ]*: Matches a single author/institution, ensuring it starts with an uppercase Unicode letter (\p{Lu}). Allows subsequent characters including Unicode lowercase letters (for international names), hyphens, apostrophes, slashes, and spaces for hyphenated names, possessives, and multi-word institutions.(?:(?:\/| & )[\p{Lu}][\p{Lu}\p{Ll}\-'\/ ]*)*: Handles multiple authors separated by either/or&. Each additional author also starts with an uppercase letter, and this group can repeat any number of times (including zero for single-author cases).(?:(?:,? )(?:et\.? al\.?|u\. ?a\.))?: Optional "et al."/u.a. suffix. Supports an optional comma before the space (to handle bothSmith, et al.andSmith et al.) and variations likeetal.,et al,u.a., oru. a..(?:,? )\d{4}: Matches the 4-digit year, allowing either a comma+space or just a space before it—this fixes your original issue with comma-free multi-author citations.(?=\)): Positive lookahead to ensure we stop at the closing parenthesis, preventing accidental matches of content outside the citation.
Tested Valid Matches
This pattern will correctly capture all your required cases:
- Single authors:
Smith, 2000,Smith 2001 - Multi-authors:
Smith/Miller 2001,Smith & Miller, 2001 - Hyphenated names/institutions:
Smith-Miller, 2001,Max-Planck-Society, 2001 - Names with apostrophes:
O'Neill 2001 - All-uppercase institutions:
ACME Company 2022 - Citations with "et al."/u.a.:
Smith et al. 2003,Miller & Jones, u.a. 2004
Fixing Your Original Pattern's Flaw
Your previous regex required a comma before the year, which is why it failed on comma-free multi-author cases. By replacing the fixed [,]*([ ] with the optional (?:,? ), we accommodate both scenarios while keeping the pattern concise. We also tightened the author matching to avoid greedy .*? captures that pulled in extra content.
内容的提问来源于stack exchange,提问作者kabauter

