[Tutor] Simple text transform with an RE

Ignacio Vazquez-Abrams ignacio@openservices.net
Mon, 20 Aug 2001 10:42:25 -0400 (EDT)


On Mon, 20 Aug 2001, Schmidt, Allen J. wrote:

> Hello all...
>
> I need to learn more about regular expressions. A quick answer to this
> should help...
>
> I have a text file of database field names - about 90 of them - one on a
> line ended with a line break.
> I need to build something with an RE to read each field from the file, then
> add parts to each field like "ALTER TABLE ADD ....field_name " at the
> beginning and other things to the end of the field name. So, I am trying to
> build a SQL statement from the list of field names. A few have to be changed
> by hand but not a big deal.

REs are _beyond_ overkill for this:

mapping={
'badtable':'goodtable'
}

file=open('myfile.txt','r')
lines=file.readlines()
file.close()
for i in lines:
  if i[-1:]=='\n':
    t=i[:-2]
  else
    t=i
  if mapping.has_key(t):
    t=mapping[t]
  print 'ALTER TABLE %s ADD changed timestamp' % (t, )

> Also needed...a link to RE examples. Its one thing to read the syntax and
> quite another to gleam actual uses from them. Real world examples really
> help and anything offered would be greatly appreciated.

Hmm...

Online I don't know about. I do know the O'Reilly's book,, _Mastering Regular
Expressions_ is decent, though.

> Thanks
>
> Allen

-- 
Ignacio Vazquez-Abrams  <ignacio@openservices.net>