【发布时间】:2020-01-14 02:07:43
【问题描述】:
我正在尝试使用正则表达式 (re.search) 传递变量,但我似乎无法使用下面的代码将信息成功读入 match.group(1)、(2) 等。有人可以快速看看我哪里出错了吗?
serial = 'abcdeID:11111abcdePR:22222abcde'
id = 11111
pr = 22222
match = re.search(r'ID:{0}PR:{1}'.format(id, pr), serial)
print("ID value returned = " + match.group(1))
print("PR value returned = " + match.group(2))
#output
#AttributeError: 'NoneType' object has no attribute 'group'
【问题讨论】:
-
你到底想在这里做什么?您是否只是想查看字符串
ID:11111PR:22222是否是串行的子字符串?或者你想从序列号中提取这两个数字(即你不知道id和pr是什么,并想从serial中提取它们)?
标签: python regex python-3.x