我在 python 中使用 RE 模块时遇到问题:
import re
url='http://list.xxx.com/0-20356-1-0-0-0-0-0-0-0-269036.html'
re_rule=re.compile('.+20356\-(\d{1,3})-0-0-0.+')
new_url=re_rule.sub('2 \1')
如我所愿,new_url 将是 ' http://list.xxx.com/0-20356-2-0-0-0-0-0-0-0-269036.html ',但 python 返回 new_url=2
。
我知道我在使用 re 模块时犯了错误。我犯了哪些错误以及如何纠正?
最佳答案
鉴于您的示例,为什么还要使用 re
呢?
url = 'http://list.xxx.com/0-20356-1-0-0-0-0-0-0-0-269036.html'
new_url = url.replace('-1-', '-2-')
print(new_url)
产生您正在寻找的内容:
http://list.xxx.com/0-20356-2-0-0-0-0-0-0-0-269036.html
关于python - 如何在Python中使用RE组替换字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38281928/