How to delete a line with re?

Peter Otten __peter__ at web.de
Mon Aug 18 02:22:08 EDT 2008


Peng Yu wrote:

> I want to delete the line with abc in the following program. But the
> following program does not do what I want. Can somebody let me know
> how to do it?

> file="""abcd
> efg
> hijk
> lmn
> """
> 
> regex = re.compile("^abcd$", re.MULTILINE)

> print regex.sub('', file),

What /do/ you want? If you want to remove the trailing newline

regex = re.compile("^abcd$\n?", re.MULTILINE)

might work.

Peter



More information about the Python-list mailing list