logic for String replacement in python
Question: How to replace a string in python For example If the text is 'new words are
newest n ew' and i want to replace 'new' word with 'y' If I use string.replace
command The above text will change to 'y words are yest n ew' I don't want
this to happen Ideally it should be 'y words are newest y' Could some body
throw some light on how to achieve this using python.


Answer: 
    >>> s = 'new words are newest n ew'
    >>> import re
    >>> re.sub(r'\bn\s*e\s*w\b', 'y', s)
    'y words are newest y'
    

`'\bn\s*e\s*w\b'` is a [regular
expression](https://docs.python.org/2/library/re.html)

`\b` means a word break  
`n`, `e`, `w` mean the letter "n", "e", "w" respectively  
`\s*` means zero or more whitespace characters

The regex given here may not be exactly what you need. You need to describe
the rules exactly if you need a more (or less) restrictive regex.

