简体   繁体   English

Python正则表达式替换括号内的单词

[英]Python regex replace words within brackets

How to replace word within brackets with empty string for repetitive words?如何用空字符串替换括号内的单词重复单词?

import re

resource_path = '/mdm/v1/{appId}/templates/{templateId}'
clean_resource_path = re.sub(r"\s*\{[^()]*\}$", '', resource_path)

print(clean_resource_path)

I get output as /mdm/v1/ but ideally I want output as /mdm/v1/templates .我得到输出为/mdm/v1/但理想情况下我希望输出为/mdm/v1/templates I know the my regex is replacing everything between {} with empty quotes, but I want only to the next available quote.我知道我的正则表达式正在用空引号替换{}之间的所有内容,但我只想要下一个可用的引号。

import re
resource_path = '/mdm/v1/{appId}/templates/{templateId}'
clean_resource_path = re.sub(r"/{\w*}*", '', resource_path)

print(clean_resource_path)

output输出

/mdm/v1/templates

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM