2

I am able to replace a word in a string content using the following solution in a case insensitive method

http://code.activestate.com/recipes/552726/ 
import re

class str_cir(str):
        ''' A string with a built-in case-insensitive replacement method '''

        def ireplace(self,old,new,count=0):
        ''' Behaves like S.replace(), but does so in a case-insensitive
        fashion. '''
            pattern = re.compile(re.escape(old),re.I)
            return re.sub(pattern,new,self,count)

My problem is i need to replace exactly the word i provide like

para = "Train toy tram dog cat cow plane TOY  Joy   JoyTOY"

i need to replace the word "toy" with "ham" and i get

'Train HAM tram dog cat cow plane HAM  Joy   JoyHAM'

What i need is

'Train HAM tram dog cat cow plane HAM  Joy   JoyTOY'
Rakesh
  • 81,458
  • 17
  • 76
  • 113

3 Answers3

4

Add \b to the start and end of the keyword:

pattern = re.compile("\\b" + re.escape(old) + "\\b",re.I)

\b means word boundary, and it matches the empty string at the start and end of a word (defined by sequence of alphanumeric or underscore character). (Reference)

As @Tim Pietzcker pointed out, it won't work as you might think if there are non-word (not alphanumeric and not underscore) characters in the keyword.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
2

Put \b at the beginning and ending of the regex.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

Wrap the word you're using in the regular expression with word boundaries (\b).

alex
  • 479,566
  • 201
  • 878
  • 984
  • putting `\b` at both ends would be same as putting `^` at the beginning and `$` at the end? – theharshest Jun 26 '12 at 11:01
  • @theharshest Nope, `\b` are word boundaries while `^` and `$` position at the start and end of string. – alex Jun 26 '12 at 11:02
  • but if I put `^` and `$` at start and end, then also it will match the exact re enclosed, solve the same purpose as `\b` does. Right? – theharshest Jun 26 '12 at 11:05