How to delete a line with re?
John Machin
sjmachin at lexicon.net
Mon Aug 18 03:33:57 EDT 2008
On Aug 18, 4:22 pm, Peter Otten <__pete... at web.de> wrote:
> 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.
>
Not only might work, but does work, including covering the corner
cases where the abcd line is immediately followed by (1) an empty line
(2) no newline then end-of-file. It is also more elegant [yes, even
regular expressions can be elegant] than what I came up with (see
below).
Hint for the OP: repr() is your friend.
>>> import re
>>> src="""abcd
... efg
... abcd
...
... hijk
... abcd"""
>>> expected = """efg
...
... hijk
... """
>>> print repr(src)
'abcd\nefg\nabcd\n\nhijk\nabcd'
>>> print repr(expected)
'efg\n\nhijk\n'
>>> for pattern in ["^abcd$\n?", r"^abcd(\n|\Z)"]:
... regex = re.compile(pattern, re.MULTILINE)
... actual = regex.sub('', src)
... print repr(actual)
... print actual == expected
...
'efg\n\nhijk\n'
True
'efg\n\nhijk\n'
True
>>>
Cheers,
John
More information about the Python-list
mailing list