1

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. I know the my regex is replacing everything between {} with empty quotes, but I want only to the next available quote.

1
  • re.sub(r"/\{[^}]*?\}", '', resource_path) Commented Oct 7, 2020 at 18:39

1 Answer 1

5
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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.