473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to delete a line with re?

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),
Aug 18 '08 #1
2 5473
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
Aug 18 '08 #2
On Aug 18, 4:22*pm, Peter Otten <__pete...@web.dewrote:
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
Aug 18 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
5
by: rn5a | last post by:
The .NET 2.0 documentation states the following: When using a DataSet or DataTable in conjunction with a DataAdapter & a relational data source, use the Delete method of the DataRow to remove...
4
by: jeepers | last post by:
Hi all, my problem is delete a line in a text file. the file contain -n line with user, userCode. i want to replace the line with user is egual at user, and/or insert new line but delete the...
24
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.