正则表达式不会捕获过去的\n

2024-10-06 10:24:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直试图用下面的内容清除一些数据,但是我的正则表达式不会超过\n。我不明白为什么,因为我想,*应该捕捉一切

table = POSITIONS AND APPOINTMENTS  2006  present Fellow, University of Colorado at Denver Health Sciences Center, Native Elder Research Center, American Indian and Alaska Native Program, Denver, CO  \n2002  present Assistant Professor, Department of Development Sociology, Cornell \n   University, Ithaca, NY   \n \n1999  2001   

输出=table.encode('ascii',errors='ignore').strip()

pat = r'POSITIONS.*'.format(endword)
print pat
regex = re.compile(pat)
if regex.search(output):
    print regex.findall(output)
    pieces.append(regex.findall(output))

以上返回:

['POSITIONS AND APPOINTMENTS  2006  present Fellow, University of Colorado at Denver Health Sciences Center, Native Elder Research Center, American Indian and Alaska Native Program, Denver, CO  ']

Tags: andofoutputtableregexcenternativepat
1条回答
网友
1楼 · 发布于 2024-10-06 10:24:12

.不匹配换行符,除非指定^{} (or ^{}) flag

>>> import re
>>> re.search('.', '\n')
>>> re.search('.', '\n', flags=re.DOTALL)
<_sre.SRE_Match object at 0x0000000002AB8100>

regex = re.compile(pat, flags=re.DOTALL)

相关问题 更多 >