regex - How to parse/substitute part of a string given a starting matching condition in python? -


assume have string

text='bla1;\nbla2;\nbla3;\n#endif\nbla4;' 

i want define method removes '\n' except if '\n' preceded string starting '#' or '\n' follows '#', result of process should be:

text2='bla1;bla2;bla3;\n#endif\nbla4;' 

is there simple way in python using regex?

(note: clear me how avoid \n followed #, using negative lookbehind, i.e. r'\n+(?!#)' challenge how identify \n preceded string starting #)

the challenge is: how deal positive lookbehind variable-length strings in python?

find : (#[a-z]+\\n)|\\n(?!#)

and replace : '\1'

output : bla1;bla2;bla3;\n#endif\nbla4;

demo here : http://regex101.com/r/uo8wh2

this keep \n newline chars have preceding word starting # or followed hash.

hth


Comments

Popular posts from this blog

How to access named pipes using JavaScript in Firefox add-on? -

multithreading - OPAL (Open Phone Abstraction Library) Transport not terminated when reattaching thread? -

node.js - req param returns an empty array -