考虑下面的字符串
text2 = '''
Mr. Schafer
Mr Smith
Ms Davis
Mrs. Robinson
Mr. T
'''我希望regex与全名相匹配,例如在“Schafer先生”中
使用finditer():
matches = re.finditer(r'(Mr|Ms|Mrs)\.?\s[A-Z]\w*', text2)
for match in matches:
print(match)结果:
<_sre.SRE_Match object; span=(1, 12), match='Mr. Schafer'>
<_sre.SRE_Match object; span=(13, 21), match='Mr Smith'>
<_sre.SRE_Match object; span=(22, 30), match='Ms Davis'>
<_sre.SRE_Match object; span=(31, 44), match='Mrs. Robinson'>
<_sre.SRE_Match object; span=(45, 50), match='Mr. T'>finditer()给出了我想要的结果,但不在列表中。
但是当我使用findall()时:
re.findall(r'(Mr|Ms|Mrs)\.?\s[A-Z]\w*', text2)结果:
['Mr', 'Mr', 'Ms', 'Mrs', 'Mr'],这是为什么?如何使用findall()获得我想要的结果
我想要这个结果:
['Mr. Schafer', 'Mr Smith', 'Ms Davis', 'Mrs. Robinson', 'Mr. T']发布于 2020-09-25 06:21:07
re.findall返回的列表包含:
捕获是被括号包围的正则表达式的一部分,除非使用(?:...);上下文中的?:告诉Python库不要将括号视为定义捕获。(当然,它仍然用于分组。)
因此,最简单的(也可能是最快的)解决方案是确保regex没有捕获,方法是使用(?:...)来包围标题,而不仅仅是(...)。
>>> re.findall(r'(?:Mr|Ms|Mrs)\.?\s[A-Z]\w*', text2)
['Mr. Schafer', 'Mr Smith', 'Ms Davis', 'Mrs. Robinson', 'Mr. T']您还可以显式捕获完整的名称:
>>> re.findall(r'((?:Mr|Ms|Mrs)\.?\s[A-Z]\w*)', text2)
['Mr. Schafer', 'Mr Smith', 'Ms Davis', 'Mrs. Robinson', 'Mr. T']在这种情况下,这样做没有多大意义,但是如果希望在输出中不显示模式的一部分,那么“一个捕获”表单就会很有用。
最后,您可能希望同时使用元组中的尊称和姓氏:
>>> re.findall(r'(?:(Mr|Ms|Mrs)\.?\s([A-Z]\w*))', text2)
[('Mr', 'Schafer'), ('Mr', 'Smith'), ('Ms', 'Davis'), ('Mrs', 'Robinson'), ('Mr', 'T')]发布于 2020-09-25 06:16:26
"()“部分是捕获指示器。
添加"?:“设置非捕获。
import re
text2 = '''
Mr. Schafer
Mr Smith
Ms Davis
Mrs. Robinson
Mr. T
'''
print(re.findall(r"(?:Mr|Ms|Mrs)\.?\s[A-Za-z]*w*", text2))
# ['Mr. Schafer', 'Mr Smith', 'Ms Davis', 'Mrs. Robinson', 'Mr. T']https://regexr.com/的左边有一个便笺表。
发布于 2020-09-25 06:32:58
我更喜欢finditer而不是findall。finditer返回文本中匹配对象的迭代器,而findall返回文本中匹配模式的列表。为了提高效率,生成器比列表更好,因为列表将所有数据读入内存,而层没有。要从iterator获取值,只需使用.group()。
import re
text2 = '''
Mr. Schafer
Mr Smith
Ms Davis
Mrs. Robinson
Mr. T
'''
matches = re.finditer(r'(Mr|Ms|Mrs)\.?\s[A-Z]\w*', text2)
match_list = [match.group() for match in matches]
print(match_list)https://stackoverflow.com/questions/64058532
复制相似问题