Python正则表达式不在新列表中打印内容

时间:2014-08-17 09:47:45

标签: python regex

我想使用正则表达式来提取字符串中的某些模式。

import re
a=[]
maxline="i have a Prof.John and As Maria a bike"
for line in maxline:
   res = re.findall(r'(?:Prof[.](\w+))', line)
   if res: 
      a.extend(res)
   res = re.findall(r'(?:As (\w+))', line)
   if res:
      a.extend(res)
   res = re.findall(r'\w+(?==\w)', line)
   if res:
      a.extend(res)
   print res

预期产出:

John
Maria

相反,我输出为:

[]
[]
[]
[]
[]
[]
[]

2 个答案:

答案 0 :(得分:1)

只需你可以这样做,

>>> import re
>>> maxline="i have a Prof.John and As Maria a bike"
>>> m = re.findall(r'(?<=Prof\.)\w+|(?<=As )\w+', maxline)
>>> for i in m:
...     print i
... 
John
Maria

更新了答案以包含第三种模式

>>> maxline="i have a Prof.John and As Maria a bike=f"
>>> m = re.findall(r'(?<=Prof\.)\w+|(?<=As )\w+|\w+(?==\w)', maxline)
>>> for i in m:
...     print i
... 
John
Maria
bike

答案 1 :(得分:0)

您正在循环个别字符,而不是在线上:

>>> maxline="i have a Prof.John and As Maria a bike"
>>> for line in maxline:
...     print line
... 
i

h
a
v
e
# .... etc.

这些个别字符与您的表达不符。

maxline更改为列表;也许可以通过str.splitlines()将其拆分为换行符:

for line in maxline.splitlines():

并打印出a res

    print a