【发布时间】:2021-02-09 04:04:54
【问题描述】:
我是正则表达式的新手,想知道如何实现以下内容。例如,
我有一个带有url('Inter.ttf') 的css 文件,我的python 程序会将此网址转换为url('user/Inter.ttf')。
但是,当我尝试避免重复替换时遇到了问题。那么在使用 re.sub 替换它们时,如何使用正则表达式告诉 python url('Inter.ttf') 和 url('/hello/Inter.ttf') 之间的区别。
我试过re.sub(r"\boriginalurl.ttf\b", "/user/" + originalurl.ttf, file)。但这似乎行不通。
那么我如何告诉 python 替换整个单词
'Inter.ttf' 和 '/user/Inter.ttf' 和 '/hello/Inter.ttf' 和 '/user/hello/Inter.ttf'。
【问题讨论】:
-
尝试
re.sub('^/?','/user/',file)将导致Inter.ttf->/user/Inter.ttf;/hello/Inter.ttf->/user/hello/Inter.ttf。 regex101.com/r/ntKFKy/1