Help with substitution

Jim Richardson warlock at eskimo.com
Fri Aug 18 20:25:34 EDT 2000


On Fri, 18 Aug 2000 16:30:13 -0400, 
 Mike Fletcher, in the persona of <mfletch at tpresence.com>,
 brought forth the following words...:

>Maybe something like:
>re.sub( '\(\W*([0-9]+)\W*downto\W*([0-9]+)\)', r'[\1:\2]', 'sdfsdf(23 downto
>43)sdf' )
>
>HTH,
>Mike
>
>-----Original Message-----
>From: Salman Sheikh [mailto:ssheikh at writeme.com]
>Sent: Friday, August 18, 2000 4:10 PM
>To: python-list at python.org
>Subject: Help with substitution
>
>
>Does anybody know how I can code this?
>
>I have a file and i have to make substitutions like so:
>
>(x downto y)  becomes [x:y]
>
>where x and y any integer values.
>I don't want to write a tons of re.compile/sub commands for
>each possibility.
>
>

Rather than use a regexp, have you considered using string manipulation. 
are the x and y values always integers? If so, it's fairly easy to turn 
(1 downto 2) into [1:2]

viz:

import string
entry = '(1 downto 2)'
entry = entry[1:-1]
int1,deadword,int2=string.split(entry)
int1=int(int1)
int2=int(int2)
print '[%i:%i]' % (int1,int2)

problems:
assumes that x and y are allways present, are ints, and that there are 
no other characters than the (x word y). But it might be useful none the less

(I swear, as fun is python is, I seem to use it most for chewing data
up and spitting it out in another form.)

-- 
Jim Richardson
	Anarchist, pagan and proud of it
WWW.eskimo.com/~warlock
	Linux, because life's too short for a buggy OS.




More information about the Python-list mailing list