Hi all, I have a question about the String.replaceAll() method.
Say I have a key word: ill
And I have a long data file: ....a kill....ill.....ill.
I want to replace all "ill" with another string A, but without replace
"kill".
By using data.replaceAll(ill, A), this will replace "kill" to "kA"
also.
Tried to use data.replaceAll("[^\\w+]ill[^\\w+]", A), but nothing got
replaced, can anybody help me? thanks!
Adam - 20 Nov 2007 16:53 GMT
> Hi all, I have a question about the String.replaceAll() method.
>
[quoted text clipped - 9 lines]
> Tried to use data.replaceAll("[^\\w+]ill[^\\w+]", A), but nothing got
> replaced, can anybody help me? thanks!
You could to one of the following, search for the string ".ill."
assuming it will always have periods before and after, or you could
check based on string length.
Lasse Reichstein Nielsen - 20 Nov 2007 18:16 GMT
> Hi all, I have a question about the String.replaceAll() method.
>
[quoted text clipped - 3 lines]
> I want to replace all "ill" with another string A, but without replace
> "kill".
What letter or character is before the other "ill"'s? Are you certain
it is not "k"? What else do you know about it?
> By using data.replaceAll(ill, A), this will replace "kill" to "kA"
> also.
>
> Tried to use data.replaceAll("[^\\w+]ill[^\\w+]", A), but nothing got
> replaced, can anybody help me? thanks!
You want to replace "ill" not after a "k" with something else.
Try:
data.replaceAll("([^k]|^)ill", "$1" + A);
This replaces "<some char but not k>ill" with "<same char>"+A.
/L

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Daniel Pitts - 20 Nov 2007 19:11 GMT
> Hi all, I have a question about the String.replaceAll() method.
>
[quoted text clipped - 9 lines]
> Tried to use data.replaceAll("[^\\w+]ill[^\\w+]", A), but nothing got
> replaced, can anybody help me? thanks!
If you want to replace the full word "ill", try the word boundary matcher \b
I also suggest looking at the Java regex help.
<http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html>
data.replaceAll("\\bill\\b", "A");

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>