A Q. about re

Ivan Van Laningham ivanlan at home.com
Thu May 25 09:40:49 EDT 2000


Hi All--
Wayne wrote:
> 
> Hello,
> This not quite about re, but what I'm trying to do with re.
> I have a module name MyForm.py,  which consist of TROFF sysntax and some
> 
> names that I want to replace using re. Like so-
> 
> myForm = r """ then the
>                       sysntax for
>                       troff goes here
>                       name1  name2 """
>                                                   ^
> 
> I have another module that  builds a GUI interface
> that user can enter some information and imports MyForm.
> I then would like to take the information that the user keyed
> in and use this information to replace the values name1 and name2
> in the troff form using re.
> Now the Question - I'm getting a SyntaxError at the point of the carret.
> 
> Could somebody tell me why. Don't I have a string being assign to a
> variable?

Yes, you do, except that the syntax is:

	myForm = r"""stuff
	more stuff"""

Note the lack of a space between the r and the ".

Once you've done that, however, consider using the dictionary style of
parameter substitution rather than rolling your own with re:

adict={}
adict["name1"]="whatever"
adict["name2"]="something"

myForm=r""" then the
                       sysntax for
                       troff goes here
                       %(name1)s  %(name2)s """ % (adict)


myForm should then look like this:

""" then the
                       sysntax for
                       troff goes here
                       whatever  something """


Your parameters inside the string will be replaced with appropriate
values from the dictionary; if %(name1) appears 50 times, it will be
replaced 50 times.

<using-re-for-this-is-like-killing-a-roach-with-a-100-ton-press>-ly
y'rs,
Ivan;-)
----------------------------------------------
Ivan Van Laningham
Axent Technologies, Inc.
http://www.pauahtun.org/
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps:  Cu Chi, Class of '70
Author:  Teach Yourself Python in 24 Hours




More information about the Python-list mailing list