replace random matches of regexp

André Malo ndparker at gmail.com
Thu Sep 8 16:18:00 EDT 2011


* gry wrote:

> 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...

You can run a re.sub() with a function as replacement value. This function
then either returns the replacement or the original match based on a
weighted random value.

nd
-- 
"Umfassendes Werk (auch fuer Umsteiger vom Apache 1.3)"
                                          -- aus einer Rezension

<http://pub.perlig.de/books.html#apache2>



More information about the Python-list mailing list