我正在使用正則表達式匹配名稱繼續「博士」。但是,當我打印比賽時,他們打印列表,有些是空的。我正在尋找打印名稱。 代碼:蟒蛇打印正則表達式匹配產生空列表
import re
f = open('qwert.txt', 'r')
lines = f.readlines()
for x in lines:
p=re.findall(r'(?:Dr[.](\w+))',x)
q=re.findall(r'(?:As (\w+))',x)
print p
print q
qwert.txt:
Dr.John and Dr.Keel
Dr.Tensa
Dr.Jees
As John winning Nobel prize
As Mary wins all prize
car
tick me 3
python.hi=is good
dynamic
and precise
tickme 2 and its in it
its rapid
its best
well and easy
期望的輸出:
John
Keel
Tensa
Jees
John
Mary
實際輸出:
['John', 'Keel']
[]
['Tensa']
[]
['Jees']
[]
[]
['John']
[]
['Mary']
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
在期望的輸出中,「Jees」發生了什麼? –