我想使用正则表达式来提取字符串中的某些模式。
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
相反,我输出为:
[]
[]
[]
[]
[]
[]
[]
答案 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