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 -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -

How to understand 2 main() functions after using uftrace to profile the C++ program? -