Mailing List Archive

search and then replace
Hi,

I can't figure out how to do search, then something, then
replace. I'm hoping someone will tell me I'm being too
complicated with this code. I should be using a parser for
this, but that's beside the point.

The idea is that I want to put <xref rid=...> inside,
<name>, but only when <name> is in <front>. RID is
grabbed from a dictionary keyed on names. So, I grab all
of <front> and do search and replace for each <name> in
<front>:

# I can assume that all of <front> will be on a single
# line.
#
# lines and ids_by_name are global
def wiggle_snoot():
name_rex = re.compile('<name>(.*?)</name>')
front_rex = re.compile('<front>(.*?)</front>')
split_line = [0,0,0]
split_sub = [0,0,0]
for i in range(0, len(lines)):
m = front_rex.search(lines[i])
if not m: continue

split_line[0] = lines[i][:m.start()]
split_line[1] = lines[i][m.start():m.end()]
split_line[2] = lines[i][m.end():]

start = 0
while 1:
m = name_rex.search(split_line[1], start)
if not m: break

split_sub[0] = split_line[1][:m.start()]
split_sub[1] = split_line[1][m.start():m.end()]
split_sub[2] = split_line[1][m.end():]

if ids_by_name.has_key(m.group(1)):
split_sub[1] = ('%s%s%s' %
('<name><xref rid="%s">' %
(ids_by_name[m.group(1)]),
m.group(1),
'</xref></name>'))
split_line[1] = join(split_sub, '')
start = len(split_sub[0]) + len(split_sub[1])
lines[i] = join(split_line, '')