Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于文本文件名称提取的正则表达式_Python_Regex - Fatal编程技术网

Python 用于文本文件名称提取的正则表达式

Python 用于文本文件名称提取的正则表达式,python,regex,Python,Regex,我有一个包含作者和摘要列表的纯文本文件,我正试图提取作者姓名,用于网络分析。我的文本遵循这种模式,包含500多篇摘要: 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 A

我有一个包含作者和摘要列表的纯文本文件,我正试图提取作者姓名,用于网络分析。我的文本遵循这种模式,包含500多篇摘要:

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.
我正在使用Python 2.7.6和re库

我试过了

regex = re.compile(r'( [A-Z][a-z]*,+)')
print regex.findall(text)
它只提取姓氏,加上摘要中逗号之前的大写单词

使用
(r'.*,')
可以完美地提取全名,但也可以获取我不需要的整个摘要


也许正则表达式是错误的方法?任何帮助或想法都将不胜感激

如果您试图匹配名称,我会尝试匹配整个子字符串,而不是其中的一部分

您可以使用以下正则表达式,并根据需要对其进行修改

>>> 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']
|

试试这个

[A-Za-z]* ?([A-Za-z]+.) [A-Za-z]*(?:,+)

它使中间名成为可选名称,并且将逗号放在非捕获组中,从而将其从结果中排除

,这正是我想要的。谢谢@hwnd我喜欢你用工作演示和解释的链接来构建你的帖子的方式。似乎每样东西都有一点吸引人。