python - 正则表达式 python \1 \2 将字符串添加到结果文本的可能性
问题描述
我很想知道是否可以在 python 中的 re.search 操作的结果中添加一个字符串。
例如,我有字符串:
strExamp = "Julia the happy girl from the 6th century"
我想要达到的结果是:
来自久违的 6 世纪的快乐女孩 Julia
我的代码:
pattern = r"([a-zA-Z ]*),([/d][a-zA-Z ]*)",r"\1, myText+ \2" --doesnt work ofc
result = re.search(pattern, strExamp)
解决这个问题的方法是什么?
解决方案
IIUC,用途:
import re
result = re.sub(r"(.*?)\s(\d+.*)", rf"\g<1> {my_text} \g<2>", strExamp)
print(result)
如果my_text = "long-gone"
和strExamp = "Julia the happy girl from the 6th century"
这打印:
Julia the happy girl from the long-gone 6th century
推荐阅读
- oracle - 如何在具有某些首字母的新表中插入所有行(pl/sql)
- sql - 如何替换 CLOB 列中的 TAB 字符?
- sql - 我正在尝试更新一列中的多个值
- android - 如何在bottomNavingationView(footer)中放大android中添加的矢量资产
- jenkins - 在 Jenkins 矩阵作业的后期构建中使用 ssh 凭据
- python - 为什么使用 TypeError: __init__() 得到一个意外的关键字参数“num_workers”?
- powerapps - 在集合中查找值
- javascript - js fetch post 工作,但 php 是空的
- xamarin.forms - 取消/订阅 Xamarin 内容视图中的事件
- r - 如何在 rmarkdown 到 beamer pdf 的特定幻灯片中包含所有参考书目