String Replace only if whole word?

Carsten Haese carsten at
Fri Nov 17 17:03:22 CET 2006

On Fri, 2006-11-17 at 10:43 -0500, Michael Yanowitz wrote:
> >Your pattern would be "[^A-Za-z0-9_]word[^A-Za-z0-9_]"
> >
> >--
> >Juho Schultz
> Thanks.
>   This works great except for one thing:
> The character after the replacement is deleted, so that if I have
>      send_data (LAPTOP, test_string)
>     it would replace it with:
>      send_data ( test_string)
>   (ignoring that the is not quoted). The comma is missing.

If you actually wanted to use string replacement, you'd need a pattern
with look-behind and look-ahead assertions along the lines of

Then again, I don't think string replacement is the right tool for the
job. You're saying that the file you are operating on in this fashion is
a Python script. Why don't you just place something like
LAPTOP="" at the top of that file, or have the script read this
information from a configuration file?


More information about the Python-list mailing list