python - python字符串在某个字符之前更改字符串中的字符
问题描述
我有这个 url,想将 px 值从 160 更改为 500。如果不知道字符的索引,我该怎么做?我用替换功能试过了
https://someurl.com//img_cache/381a58s7943437_037_160px.jpg?old
我想要的是:
https://someurl.com//img_cache/381a58s7943437_037_500px.jpg?old
解决方案
此处的正则表达式\d+(?=px)
查找前面的数字,px
然后将它们替换为您在参数中输入的任何内容new_res
。
import re
string = "https://someurl.com//img_cache/381a58s7943437_037_160px.jpg?old"
new_res = "500"
out = re.sub("\d+(?=px)", new_res, string)
print(out)
输出:
https://someurl.com//img_cache/381a58s7943437_037_500px.jpg?old
推荐阅读
- node.js - 当包 X 作为依赖项安装时会运行什么 npm 脚本,但当我在包 X 上运行 npm install 时不会运行?
- matlab - 将数组处理为函数参数
- r - 如何从所有图中有选择地打印图
- spring - Spring Boot + 多数据源问题 + 生产集群环境
- python-imaging-library - 使用 mss 调整屏幕截图的大小,以便使用 pytesseract 更好地阅读
- r - R rtweet:如果给定 Twitter 句柄没有返回结果,则 search_tweets 循环不会继续
- ios - 在刷新之前,第一个 Firebase 查询项出现问题
- sql - 如何用相同 PolicyNumber 的非空值替换空字符串
- javascript - AngularJS单击时突出显示锚标记
- html - Bootstrap 4:高度相等的 div,中间有颜色填充和装订线