python-3.x 正则表达式在单词前添加字符

qlvxas9a  于 2022-12-15  发布在  Python
关注(0)|答案(2)|浏览(163)
import re
text = 'https://app.propertymeld.com/1568/v/23256/meld/4376491/'
d = re.sub(r'(meld)', r'\1s', text)
e = re.sub(r'api',r'(/meld)', d)
print(e)

所需溶液:“https://app.propertymeld.com/1568/v/23256/api/melds/4376491/tenant-files/“
有没有办法在单词meld之前加上单词API。

ykejflvf

ykejflvf1#

尝试:

import re

text = 'https://app.propertymeld.com/1568/v/23256/meld/4376491/'

e = re.sub(r'/meld', '/api/melds', text)

print(e)

输出:

https://app.propertymeld.com/1568/v/23256/api/melds/4376491/

备注:

如果希望tenant-files/位于末尾,则

e = re.sub(r'/meld', '/api/melds', text) + "tenant-files/"
输出:
https://app.propertymeld.com/1568/v/23256/api/melds/4376491/tenant-files/
rsaldnfx

rsaldnfx2#

另一条路在这里。

import re
text = 'https://app.propertymeld.com/1568/v/23256/meld/4376491/'
e = re.sub(r'(/meld)', r'/api\1s', text)
print(e)

输出

https://app.propertymeld.com/1568/v/23256/api/melds/4376491/

相关问题