我输入了软件和软件版本(仅是示例):
- Oracle 1.1 数据库
- Oracle 2.2
- Oracle 3.3 平台
- Oracle 数据库 4.4
- 甲骨文50.50
我想要的输出是:
- 甲骨文1.1
- Oracle 2.2
- Oracle 3.3
- Oracle 4.4
- 甲骨文50.50
我可以成功获得其中 5 个中的 4 个
re.compile('Oracle \d*.\d*')
问题是“Oracle DB 4.4”。如何让正则表达式忽略“DB”并仅返回“Oracle 4.4”,但仍然适用于其余输入?
最佳答案
您可以使用 2 个捕获组,其中 DB
是可选的
(Oracle )(?:DB )?(\d+(?:\.\d+))
如果小数部分不可选:
(Oracle )(?:DB )?(\d+\.\d+)
在替换中使用 2 个捕获组。
result = re.sub(regex, r"\1 \2", test_str)
关于python - 如何从正则表达式结果中删除不必要的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63727934/