replace linebreak

Pedro Rodriguez pedro_rodriguez at club-internet.fr
Thu Jan 23 12:47:16 EST 2003


On Thu, 23 Jan 2003 18:18:04 +0100, Achim Domma wrote:

> Hi,
> 
> I try to replace line breaks using the re module, but can not figure out
> how to do it. If a line does not end with a ')' I want to join it with
> the following line. I tried something like
> 
> expr = re.compile(r'[^)]\n',re.M)
> expr.sub('',file_content))
> 
> but it does not change anything.
> 

Are you sure ? This is what I actually get with your example :

>>> expr = re.compile(r'[^)]\n',re.M)
>>> expr.sub('', '(Hello\nWorld)\n end of text')
'(HellWorld)\n end of text'


The problem I see is that you lose the character before the '\n',
so this could be better I think :

>>> expr = re.compile(r'([^)])\n',re.M)
>>> expr.sub('\g<1>', '(Hello\nWorld)\n end of text')
'(HelloWorld)\n end of text'


Pedro




More information about the Python-list mailing list