views:

161

answers:

1

Hi,

I might be asking this question incorrectly but what I would like to do is the following:

Given a large String which could be many 100s of lines long match and replace a word exactly and make sure it does not replace and match any part of any other String.

For example :

Strings to Find = Mac Apple Microsoft Matt Damon I.B.M. Hursley  
Replacement Strings = MacO AppleO MicrosoftO MattDamonP I.B.M.O HursleyL  
Input String (with some of the escape characters included for clarity) =  
"A file to test if it finds different\r\n  
bits and bobs like Mac, Apple and Microsoft.\n  
I.B.M. in Hursley does sum cool stuff!Wow look it's "Matt Damon"\r\n  
Testing something whichwillerrorMac"\n  

OUTPUT

"A file to test if it finds different  
bits and bobs like MacO, AppleO and MicrosoftO.  
I.B.M.O in HursleyL do sum cool stuff!Wow look it's "Matt DamonP"  
Testing something whichwillerrorMac"  

I have tried using Regex using word boundaries, although this picks up 'whichwhillerrorMacO' on the last line.

I have also tried using the StringTokenizer class and various delimiters to try and replace words, but some of the words I am trying to replace contains these delimiters.

Is there a regex that would solve this problem?

+2  A: 

Replacing \b(Mac|Apple)\b with \$1O\ will not touch whichwillerrorMac - it will match whichwill-Mac though.

Amarghosh