python - 如何将多个变量传递给常规(re.search)表达式
问题描述
我正在尝试使用正则表达式(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'
解决方案
您的正则表达式中没有组(...)
,因此不会返回它们。像这样添加它们:
match = re.search(r'ID:({0})PR:({1})'.format(id, pr), serial)
推荐阅读
- javascript - 从授权服务器收到无效响应。Xero 与 Bagisto 的集成
- python - 产生这个 {20702 20709 20695 20703} 的方程来产生这个 {20714}
- python - 如何抓取 HTML 按钮数据?
- bash - 如何在 PyCharm/IntelliJ 中自动“chmod”具有特定扩展名的文件?
- swift - 如何在 SwiftUI 的 AnyPublisher 中设置元组
- python - Pandas:如何自动创建循环以为不同组制作多个列联表?
- javascript - 使用 WebStorm 时出现“ReferenceError:文档未定义”
- python - Django 项目只能在本地运行,不能在 Heroku 上运行?
- html - 如何在具有不同 ID 的 HTML 中多次插入相同的选择对象?
- python - 一次处理 N 行 Boto3 流式响应