Main Content

Flexible search and replace in Python

Archive - Originally posted on "The Horse's Mouth" - 2010-03-25 06:09:36 - Graham Ellis

There are various different ways you may want to search and replace within a string - and this post shows you how a lot of them work in Python.

1. You may want to replace one literal piece of text by another

2. You may want to replace a string matching a pattern by a literal string

3. You may want to replace a string that matches a pattern by a new string that contains all of (or elements of) the string that matched

4. You may want to replace a string that matches a pattern by a new string that's based on running a piece of code on that matched string.

Replacing one literal string by another

>>> text = "The pea soup club"
>>> text.replace("pea","vegetable")
'The vegetable soup club'
>>>


Replacing a matched string by a literal

This and the following example all use a regular expression:

import re
digits = re.compile(r'(\d+)')


Simply give the replacement string and the incoming string as parameters to the sub method on the regular expression object:

highprice = digits.sub("99",cost)
print highprice


Replacing a matched string by one containing part of the match

* Mark the bit of the incoming string you want to use in the output with capture parentheses

* Use {} for the first capture (etc) in the output string in your sub

highprice = digits.sub(r'** {} **',cost)
print highprice


Replace a matched string by the result of running code on that match

The example I have used here is an example which takes a numeric vaklue in a string and replaces it by a value that's 5 more; this can't by a simple string replacement ... 17 + 5 => 22 which isn't a straightforward string operations, even in Python.

* Mark the bit of the incoming string you want to calculate on for the output with capture parentheses

* Define a function that converts your captured group(s) into a new string which it returns

* use the "runnable" - i.e. the function - in place of the more usual output string object in your sub

def action(val):
   newval = "%d" % (int(val.group()) + 7)
   return newval
highprice = digits.sub(action,cost)
print highprice


There's a complete source code example from yesterday's Python Programming Course - see [here].