StringReplace help?

Hi,

I have a list of strings and I want to replace several substrings in it. For example, take “A CO IN”, I want to end up with only “A”. I’m using

StringReplace["A CO IN",{Whitespace~~"IN"~~(Whitespace|EndOfString)->" ",Whitespace~~"CO"~~(Whitespace|EndOfString)->" "}]

The problem I have is that I’m ending up with “A IN”, I checked the documentation and it is because after replacing the first pattern, it continues searching at the next character after the pattern. Therefore Whitespace~~“IN”~~(Whitespace|EndOfString) will never be matched because it continues searching at “I” after replacing " CO ". Can you help me accomplish my task?

One way is to use FixedPoint with a pure function as in:

FixedPoint[
 StringReplace[#, {Whitespace ~~ "IN" ~~ (Whitespace | EndOfString) ->
      " ", Whitespace ~~ "CO" ~~ (Whitespace | EndOfString) -> 
     " "}] &, "A CO IN"]

Thanks a lot. Excellent answer as always, David.