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

'hasOwnProperty' in javascript -

python - ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'> -

java - How to provide dependency injections in Eclipse RCP 3.x? -