Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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,我不熟悉正则表达式,我有一个电话簿。我想从中提取名字。我写了这篇文章(如下),但它提取了很多不需要的文本,而不仅仅是名字。你能告诉我我做错了什么以及如何改正吗?这是我的密码: import re directory = '''Mark Adamson Home: 843-798-6698 (424) 345-7659 265-1864 ext. 4467 326-665-8657x2986 E-mail:madamson@sncn.net Allison Andrews Home: 612-3

我不熟悉正则表达式,我有一个电话簿。我想从中提取名字。我写了这篇文章(如下),但它提取了很多不需要的文本,而不仅仅是名字。你能告诉我我做错了什么以及如何改正吗?这是我的密码:

import re

directory = '''Mark Adamson
Home: 843-798-6698
(424) 345-7659
265-1864 ext. 4467
326-665-8657x2986
E-mail:madamson@sncn.net
Allison Andrews
Home: 612-321-0047
E-mail: AEA@anet.com
Cellular: 612-393-0029
Dustin Andrews'''


nameRegex = re.compile('''
(
[A-Za-z]{2,25}
\s
([A-Za-z]{2,25})+
)

''',re.VERBOSE)

print(nameRegex.findall(directory)) 

它给出的输出是:

[('Mark Adamson', 'Adamson'), ('net\nAllison', 'Allison'), ('Andrews\nHome', 'Home'), ('com\nCellular', 'Cellular'), ('Dustin Andrews', 'Andrews')]

非常感谢您的帮助

您的问题是
\s
也将匹配换行符。只需添加一个空格,而不是
\s
。就是

name_regex=re.compile('[A-Za-z]{2,25}[A-Za-z]{2,25}')
如果名称正好有两个单词,则此操作有效。如果名称包含两个以上的单词(中间名或连字符的姓氏),则您可能希望将其扩展为:

name_regex=re.compile(r“^([A-Za-z\-]{2,25})+$”,re.MULTILINE)

这将查找一个或多个单词,并将从一行的开头延伸到结尾(例如,将不只是从“John Paul Jones”中获取“John Paul”)

我可以建议尝试下一个正则表达式,它适合我:

"([A-Z][a-z]+\s[A-Z][a-z]+)"

以下正则表达式按预期工作

代码的相关部分:

nameRegex = re.compile(r"^[a-zA-Z]+[',. -][a-zA-Z ]?[a-zA-Z]*$", re.MULTILINE)

print(nameRegex.findall(directory) 
>>> python3 test.py 
['Mark Adamson', 'Allison Andrews', 'Dustin Andrews']
输出:

nameRegex = re.compile(r"^[a-zA-Z]+[',. -][a-zA-Z ]?[a-zA-Z]*$", re.MULTILINE)

print(nameRegex.findall(directory) 
>>> python3 test.py 
['Mark Adamson', 'Allison Andrews', 'Dustin Andrews']
尝试:


这将只选择由两个或多个由“word”字符组成的完整行。

r'[A-Za-z]{2,25}[A-Za-z]{2,25}'
如果您喜欢速记。请记住,它不会固定在行的开头和结尾。如果只想用两个单词匹配整行,请分别在正则表达式的开头和结尾添加
^
$
,然后启用。