regex - how to get specific str in python using regular expression? -
i have str rjg[]u[ur"fur[ufrng[]"gree,
and want replace "[" , "]" between "" #,the result
rjg[]u[ur"fur[ufrng[]"gree => rjg[]u[ur"fur#ufrng##"gree,
how can in python?
one liner solution:
import re text = 'rjg[]u[ur"fur[ufrng[]"gree' text = re.sub(r'(")([^"]+)(")', lambda pat: pat.group(1)+pat.group(2).replace(']', '#').replace('[', '#')+pat.group(3), text) print text output:
rjg[]u[ur"fur#ufrng##"gree
Comments
Post a Comment