python - re.sub not working for a particular regex pattern -


i using following code :

re.sub(inputpattern,outputpattern, currentline)

in above code , reading value of outputpattern csv , value :

\\1-\\2-\\3-\\4 

i reading below :

outputpattern = row['prefix_1_wrt_fmt']

i have tried :

outputpattern = "'"+ row['prefix_1_wrt_fmt'] +"'"

the problem is not treating proper format , if hard code below works fine :

re.sub(inputpattern,'\\1-\\2-\\3-\\4', currentline)

you need escape backslashes if have literal string.

"\\1-\\2-\\3-\\4" 

if read input don't have that. need change pattern inside csv \1-\2-\3-\4

you use raw string if dislike escaping every char when using literal string, prefixing string letter r.

r"\1-\2-\3-\4" 

Comments

Popular posts from this blog

javascript - Wordpress slider, not displayed 100% width -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -