replace random matches of regexp

gry georgeryoung at gmail.com
Thu Sep 8 21:51:17 CEST 2011


[Python 2.7]
I have a body of text (~1MB) that I need to modify.   I need to look
for matches of a regular expression and replace a random selection of
those matches with a new string.  There may be several matches on any
line, and a random selection of them should be replaced.  The
probability of replacement should be adjustable.  Performance is not
an issue.  E.g: if I have:

SELECT max(PUBLIC.TT.I) AS SEL_0 FROM (SCHM.T RIGHT OUTER JOIN
PUBLIC.TT ON (SCHM.T.I IS NULL)) WHERE (NOT(NOT((power(PUBLIC.TT.F,
PUBLIC.TT.F) = cast(ceil((         SELECT 22 AS SEL_0        FROM
(PUBLIC.TT AS PUBLIC_TT_0 JOIN PUBLIC.TT AS PUBLIC_TT_1 ON (ceil(0.46)
=sin(PUBLIC_TT_1.F)))        WHERE ((zeroifnull(PUBLIC_TT_0.I) =
sqrt((0.02 + PUBLIC_TT_1.F))) OR

I might want to replace '(max|min|cos|sqrt|ceil' with "public.\1", but
only with probability 0.7.  I looked and looked for some computed
thing in re's that I could stick and expression, but could not find
such(for good reasons, I know).
Any ideas how to do this?  I would go for simple, even if it's wildly
inefficient, though elegance is always admired...



More information about the Python-list mailing list