I've encountered a problem iwhile writing a find and replace prg in python.
this is my code:

import re
f = file('testfile')
while True:
	line = f.readline()
	if len(line) == 0:
	print line
	p = re.compile(line)
	m = p.match('METASERV_HOME')
	print m

as per the logic :
if my testfile contains METASERV_HOME, then i shld be getting an instance of "matching object"
otherwise , "none"
but instead of the former result, i am getting just none.
i want to know if i have done something wrong in reading a file.
print line....in the prg correctly reads the first line in the textfile.
for testing pupose my textfile has only 2 lines in the file and both of them are METASERV_HOME.

here is the output:

C:\python>python findnreplace.py



so where wud the problem be.?
kindly help me on this,
Thanks in advance,

Edit: Added code tags vegaseat


METASERV_HOME is the string you want to find, so you have to re.compile("METASERV_LINE"). Then you ... I'll just show you my snippet :)

import re

p = re.compile("METASERV_HOME")
f = file("d.dat", "r")
for line in f:
    m = p.match(line)
    print m

If you just want to know if "ME...." is in your line, then you don't need a regex. You can just write

for line in f:
    if "METASERV_HOME" in line: 

If you just want to replace it with something else, it gets even easier:

for line in f:
    line = line.replace("METASERV_HOME", "NEW")
    print line

Hope this helps.

Regards, mawe

This question has already been answered. Start a new discussion instead.