[Catalog-sig] Help with Python Regex's

Graeme Matthew graeme@sofcom.com.au
Thu, 17 May 2001 11:27:13 +1000


This is a multi-part message in MIME format.

------=_NextPart_000_0009_01C0DEC4.5202B7C0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I am very new to python (as im a perl developer, sorry !) and am still =
trying to get my head around the regular expression model.
Python does not seem to have perls variable interpolation (or does it ?)

I have an HTML file, example below:

<HTML>
<HEAD>
<BODY>
This is a test of things<BR>
<!-- Replace Me -->
</BODY>
</HTML>

This is my template file. I want to open it and replace the <!-- Replace =
Me --> value with a new value:

Heres my code:

import re

f =3D open("C:\\www.timemanager.com\\test.html")
fileContent =3D f.read()
f.close

newVal =3D "Heres the replacement code which means it worked"
key =3D "Replace Me"

re.sub('\<!--\s*' + key + '\s*--\>',newVal,fileContent)

print fileContent

raw_input("ok done")=20

it wont work, please can someone assist in explaining where I am going =
wrong, thanks a mill





Graeme Matthew - Internet Programmer
Software Communication Group
Como Centre, 644 Chapel Street
South Yarra, Melbourne
Victoria
Mobile: 0412 806 476

This message contains privileged and confidential information intended =
only
for the use of the addressee named above.  If you are not the intended
recipient of this message you must not disseminate, copy or take any =
action
in reliance on it.  If you have received this message in error, please
notify Software Communication Group immediately.

Any views expressed in this message are those of the individual sender
except where the sender specifically states them to be the views of =
Software
Communication Group.



------=_NextPart_000_0009_01C0DEC4.5202B7C0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.3103.1000" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DTahoma size=3D2>I am very new to python (as im a perl =
developer,=20
sorry !) and am still trying to get my head around the regular =
expression=20
model.</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2>Python does not seem to have perls =
variable=20
interpolation (or does it ?)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>I have an HTML file, example =
below:</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma=20
size=3D2>&lt;HTML&gt;<BR>&lt;HEAD&gt;<BR>&lt;BODY&gt;<BR>This is a test =
of=20
things&lt;BR&gt;<BR>&lt;!-- Replace Me=20
--&gt;<BR>&lt;/BODY&gt;<BR>&lt;/HTML&gt;</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>This is my template file. I want to =
open it and=20
replace the &lt;!-- Replace Me --&gt; value with a new =
value:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>Heres my code:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>import re</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>f =3D=20
open("C:\\www.timemanager.com\\test.html")<BR>fileContent =3D=20
f.read()<BR>f.close</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>newVal =3D "Heres the replacement code =
which means=20
it worked"<BR>key =3D "Replace Me"</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>re.sub('\&lt;!--\s*' + key +=20
'\s*--\&gt;',newVal,fileContent)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>print fileContent</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>raw_input("ok done") </FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>it wont work, please can someone =
assist in=20
explaining where I am going wrong, thanks a mill<BR></DIV></FONT>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>Graeme Matthew - Internet =
Programmer<BR>Software=20
Communication Group<BR>Como Centre, 644 Chapel Street<BR>South Yarra,=20
Melbourne<BR>Victoria<BR>Mobile: 0412 806 476</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>This message contains privileged and =
confidential=20
information intended only<BR>for the use of the addressee named =
above.&nbsp; If=20
you are not the intended<BR>recipient of this message you must not =
disseminate,=20
copy or take any action<BR>in reliance on it.&nbsp; If you have received =
this=20
message in error, please<BR>notify Software Communication Group=20
immediately.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>Any views expressed in this message =
are those of=20
the individual sender<BR>except where the sender specifically states =
them to be=20
the views of Software<BR>Communication Group.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_0009_01C0DEC4.5202B7C0--