Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

replace exactly the same string?

Thread view: 
au.danji@gmail.com - 20 Nov 2007 16:23 GMT
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/>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.