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

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -