【发布时间】:2020-11-28 10:02:55
【问题描述】:
我正在尝试使用正则表达式正向查找创建字典列表。我尝试了两种不同的代码:
变体 1
string = '146.204.224.152 - lubo233'
for item in re.finditer( "(?P<host>[0-9]*[.][0-9]*[.][0-9]*[.][0-9]*)(?P<user_name>(?<= - )[a-z]*[0-9]*)", string ):
print(item.groupdict())
变体 2
string = '146.204.224.152 - lubo233'
for item in re.finditer( "(?P<host>[0-9]*[.][0-9]*[.][0-9]*[.][0-9]*)(?<= - )(?P<user_name>[a-z]*[0-9]*)", string ):
print(item.groupdict())
期望的输出
{'host': '146.204.224.152', 'user_name': 'lubo233'}
问题/问题
在这两种情况下,我都无法消除子字符串“-”。
使用积极的后视(?<= - ) 会使我的代码出错。
谁能帮助找出我的错误?谢谢。
【问题讨论】:
标签: python regex dictionary positive-lookbehind