如何将多个变量传递给正则表达式(检索)表达式

2024-10-02 08:25:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用正则表达式传递变量(检索)但我似乎无法使用下面的代码成功地将信息读入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'

Tags: 代码re信息idvaluematchserialgroup

热门问题