用于文本文件名称提取的正则表达式 [英] Regex for name extraction on text file
问题描述
我有一个包含作者和摘要列表的纯文本文件,我正在尝试仅提取作者姓名以用于网络分析.我的文本遵循此模式并包含 500 多个摘要:
2010 - 洛斯阿拉莫斯特殊核材料的核取证:最近的三项研究购买这篇文章David L. Gallimore,洛斯阿拉莫斯国家实验室Katherine Garduno,洛斯阿拉莫斯国家实验室Russell C. Keller,洛斯阿拉莫斯国家实验室特殊核材料的核取证是一个高度专业化的领域,因为世界上很少有分析实验室可以安全地处理核材料,使用经过验证的分析方法进行高精度和精密分析.
我使用 Python 2.7.6 和 re 库.
我试过了
regex = re.compile(r'( [A-Z][a-z]*,+)')打印 regex.findall(text)
仅提取姓氏,以及摘要中逗号前的所有大写单词.
使用 (r'.*,')
可以完美地提取全名,但也可以获取我不需要的整个摘要.
也许正则表达式是错误的方法?任何帮助或想法表示赞赏.
如果您要匹配名称,我会尝试匹配整个子字符串而不是其中的一部分.
您可以使用以下正则表达式并根据需要进行修改.
<预><代码>>>>regex = re.compile(r'\b([A-Z][a-z]+(?: [A-Z]\.)? [A-Z][a-z]+),')>>>打印 regex.findall(text)['David L. Gallimore'、'Katherine Garduno'、'Russell C. Keller']I've got a plain text file containing a list of authors and abstracts and I'm trying to extract just the author names to use for network analysis. My text follows this pattern and contains 500+ abstracts:
2010 - NUCLEAR FORENSICS OF SPECIAL NUCLEAR MATERIAL AT LOS ALAMOS: THREE RECENT STUDIES
Purchase this article
David L. Gallimore, Los Alamos National Laboratory
Katherine Garduno, Los Alamos National Laboratory
Russell C. Keller, Los Alamos National Laboratory
Nuclear forensics of special nuclear materials is a highly specialized field because there are few analytical laboratories in the world that can safely handle nuclear materials, perform high accuracy and precision analysis using validated analytical methods.
I'm using Python 2.7.6 with the re library.
I've tried
regex = re.compile(r'( [A-Z][a-z]*,+)')
print regex.findall(text)
Which pulls out the last names only, plus any capitalized words prior to commas in the abstracts.
Using (r'.*,')
works perfectly to extract the full name, but also grabs the entire abstract which I don't need.
Maybe regex is the wrong approach? Any help or ideas are appreciated.
If you are trying to match the names, I would try to match the entire substring instead of part of it.
You could use the following regular expression and modify it if needed.
>>> regex = re.compile(r'\b([A-Z][a-z]+(?: [A-Z]\.)? [A-Z][a-z]+),')
>>> print regex.findall(text)
['David L. Gallimore', 'Katherine Garduno', 'Russell C. Keller']
这篇关于用于文本文件名称提取的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!