python - 使用正则表达式删除子字符串
问题描述
我需要编写一个正则表达式来从下面的字符串中提取末尾的金额
输入: Total: 4.00 4,123.00
输出应该像:Total: 4,123.00
这意味着我需要用它删除“4.00”。
我的python
代码:
import re
text = 'Total: 4.00 4,123.00'
m = re.search('4.00 (.+?)$', text)
if m:
found = m.group(1)
print(found)
但是这个正则表达式也删除Total:
了。我该如何解决?
解决方案
我会在这里使用 split 并避免使用正则表达式:
inp = "Total: 4.00 4,123.00"
parts = inp.split()
output = parts[0] + ' ' + parts[2]
print(output) # Total: 4,123.00
推荐阅读
- gradle - Gradle 的 Script 对象 Logger 配置在哪里?
- python - /admin/budget/expense/add/ 处的 OperationalError
- python - TclError:无效的命令名称“.!frame.!canvas”
- thingsboard - Thingsboard 电子邮件未发送警报
- android - 由于 gradle 文件中的错误,构建失败
- javascript - 浏览器后退/前进按钮加载多个 CKEditor 表单
- amazon-web-services - 如何在 CloudFormation 模板中引用现有 AWS 证书?
- firebase - 每个 X 周期用函数替换 firebase 数据?
- vba - 如何填写网页上的文本字段?
- django - 支持使用 HTTP / REST 下载部分文件