On Mon, 18 Jan 2010 14:43:46 +0100, superpollo <>
declaimed the following in gmane.comp.python.general:
>
> i guess that the algorithm would be easier if it was known in advance
> that the string to substitute must have some specific property, say:
>
> 1) they all must start with "XYZ"
> 2) they all have the same length N (e.g. 5)
>
That now seems to conflict with your previous sample where old=>new
terms were different lengths.
The original description is one in which I'd probably have done a
series of .split()/.join() operations, using some sort of marker string
that is not valid for the original input to hold the position of
"old"... (repeat for each "old", with unique markers) Then repeating the
..split/.join replacing the markers with the proper "new" strings...
So how do you combine item 1 above with the prior multiple
replacements?
-=-=-=-=-=-=-=-=-
INPUT = "qweXYZ12asdXYZ1345XYZ"
OLD = "XYZ"
NEW = "IWAS"
MINLEN = 5
res = []
oldlen = len(OLD)
tail = MINLEN - oldlen
parts = INPUT.split(OLD)
res.append(parts[0])
for term in parts[1:]:
if len(term) >= tail:
res.append(NEW + term)
else:
res.append(OLD + term)
output = "".join(res)
print "'%s'" % output
-=-=-=-=-=-=-=-=-=-
'qweIWAS12asdIWAS1345XYZ'
--
Wulfraed Dennis Lee Bieber KD6MOG
HTTP://wlfraed.home.netcom.com/