san*_*osh 1 python regex python-3.x python-re
如何用空字符串替换括号内的单词重复单词?
import re
resource_path = '/mdm/v1/{appId}/templates/{templateId}'
clean_resource_path = re.sub(r"\s*\{[^()]*\}$", '', resource_path)
print(clean_resource_path)
Run Code Online (Sandbox Code Playgroud)
我得到输出,/mdm/v1/但理想情况下我希望输出为/mdm/v1/templates. 我知道我的正则表达式正在用{}空引号替换它们之间的所有内容,但我只想要下一个可用的引号。
小智 5
import re
resource_path = '/mdm/v1/{appId}/templates/{templateId}'
clean_resource_path = re.sub(r"/{\w*}*", '', resource_path)
print(clean_resource_path)
Run Code Online (Sandbox Code Playgroud)
输出
/mdm/v1/templates
Run Code Online (Sandbox Code Playgroud)