I’m a Python beginner.
I’d like to find <(.+?)> from a string, and replace it with [\1].
For example,
string_input = '<age>'
string_output = '[age]'
I tried,
import re
string = '<age>'
re.sub('<.+?>, '[' + \1 + ']', string)
But it failed.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Double
\\is used to escaping the\, or else\1will be recognised as\x01.Brackets
()are used as a capturing group.You can use multiple capturing groups like this: