Replacing pieces of text

Simple replacement

Simple replacement means replacing all occurrences of a piece of text which is determined by an exact character-by-character match (as opposed to glob patterns or regular expressions).

The tl.rename.replace module defines a function named simple_replace that performs simple replacement. Like all tl.rename transformation functions, it accepts an iterable of old file names and arbitrary keyword options. The replace option’s value is an iterable of replacement pairs:

>>> from tl.rename.replace import simple_replace
>>> simple_replace(['foobar', 'foobarfoo'], replace=[('foo', 'baz')])
['bazbar', 'bazbarbaz']

If several pairs are given, they will be applied in order:

>>> simple_replace(['foobar'], replace=[('foo', 'bar'), ('bar', 'baz')])
['bazbaz']

In particular, replacing a substring with an empty string deletes that substring:

>>> simple_replace(
...     ['foobar'], replace=[('foo', 'bar'), ('rb', ''), ('aa', 'zz')])
['bzzr']

Table Of Contents

Previous topic

Case transformations

Next topic

Modules

This Page