How to delete a line with re?

Discussion in 'Python' started by Peng Yu, Aug 18, 2008.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    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?

    Thanks,
    Peng

    #!/usr/bin/python

    import re

    file="""abcd
    efg
    hijk
    lmn
    """

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

    print file,
    print "$"
    print regex.sub('', file),
     
    Peng Yu, Aug 18, 2008
    #1
    1. Advertising

  2. Peng Yu

    Peter Otten Guest

    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
     
    Peter Otten, Aug 18, 2008
    #2
    1. Advertising

  3. Peng Yu

    John Machin Guest

    On Aug 18, 4:22 pm, Peter Otten <> 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
     
    John Machin, Aug 18, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,389
    Matt Humphrey
    Oct 18, 2004
  2. kaushikshome
    Replies:
    4
    Views:
    822
    kaushikshome
    Sep 10, 2006
  3. scad
    Replies:
    23
    Views:
    1,218
    Alf P. Steinbach
    May 17, 2009
  4. Richard Schneeman
    Replies:
    16
    Views:
    554
    Daniel Bush
    Aug 27, 2008
  5. Marek Stepanek
    Replies:
    12
    Views:
    438
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page