regex - Python如何查找和替换未知部分或字符串
问题描述
假设我有一个字符串,我知道格式,但里面有变量,我怎样才能删除格式?前任:
s = """
<%Hello%>
<%foo%>
<%Example%>
<no
>%change
here%>
"""
print(s.replace("<%*%>", "&it works&"))
输出:
> &it works&
&it works&
&it works&
<no
>%change
here%>
解决方案
利用re.sub
前任:
import re
s = """
<%Hello%>
<%foo%>
<%Example%>
<no
>%change
here%>
"""
print(re.sub("<%(.*?)%>", "&it works&", s))
输出:
&it works&
&it works&
&it works&
<no
>%change
here%>
推荐阅读
- matlab - 如何在matlab中用第二个矩阵的每一行减去矩阵的每一行?
- go - 建模服务错误 - gRPC 与 Google API
- r - 您可以使用 .SDcols 对 data.table 中的行进行子集化吗
- python - response.html.render() 在 gcloud 函数部署中不起作用
- javascript - WordPress:Divi Accordion Trigger - 当用户选择手风琴的不同部分时,触发图像更改的最佳方式是什么?
- java - 如何在 Popup 活动后面添加模糊
- google-cloud-platform - 使用 GCP 负载平衡器创建重定向主机转发规则到不同的 URL?
- sql - 在最近的星期日之前删除数据 - SQL Snowflake
- jquery - 更新多个表单的隐藏字段时出现问题
- mysql - 如何执行 IF Then else /Case 语句以返回所需的结果