python - How to use a special sequence in substitute pattern in re.sub -


this question has answer here:

i have string, in i'd substitute each [ [[] , ] []] (at same time). thought doing re.sub:

re.sub(r'(\[|\])', '[\1]', 'asdfas[adsfasd]') out: 'asdfas[\x01]adsfasd[\x01]' 

but i'm not getting desired result -- how make re.sub consider \1 in pattern first matched special group?

you should use r prefix replacing regex well, otherwise \1 interpreted hex literal:

in [125]: re.sub(r'(\[|\])', r'[\1]', 'asdfas[adsfasd]')  out[125]: 'asdfas[[]adsfasd[]]' 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -