python - python正则表达式仅在括号中替换
问题描述
解决方案
You may use the Group Reference here.
import re
before = '4ー3ー1ーー4ー31'
after = re.sub(r'(\d+)ー(\d+)ー(\d+)', r'\1-\2-\3', before)
print(after) # '4-3-1ーー4ー31'
Here, r'\1'
is the reference to the first group, a.k.a, the first parentheses.
推荐阅读
- python - 似乎无法让 Flask_login 与 gunicorn 一起使用
- ssas-tabular - 在 PowerShell 中列出 Analysis Services 表格实例表
- c# - Microsoft Graph API C#/Core 2.1 - “提供的参数之一是不可接受的。”
- python - Python 中的异常与错误
- docker - 如何将 docker name 参数添加到 kuberntes 集群中
- php - 如何让多个并行 PHP 请求等待缓存更新?
- windows - 清单列表条目中没有与 windows/amd64 10.0.17134 匹配的清单
- java - 在 Eclipse 中为嵌入式码头服务器开发战争的推荐方法是什么?
- python - 连接到 SQL Server 并从 Python 以“直通”方式运行查询
- google-drive-api - 如何在 Google Colab 中提取一个非常大的文件