This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Replace exact words using regular expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Replace exact words using regular expressions" Watch "Replace exact words using regular expressions" New topic
Author

Replace exact words using regular expressions

Pandu Rangam
Greenhorn

Joined: Dec 08, 2006
Posts: 24
Hi all,
I have some text like
My Apartment, has Apartments where Apts is Apartment Aprartments @Apts. sdfasdfaApt Apt/No Aptasdf asdf Apts.. Apartments

from this i want replace all exact occurrences of
Apartment, Apartments, Apt, Apts,Apts., Apt/No with Some text, ex. "TEST".
After replacing text should look like this

My TEST, has TEST where TEST is TEST TEST @Apts. sdfasdfaApt TEST Aptasdf asdf TEST.
For this I need a regular expression.

Thanks in advance


SCJP 1.4,<br />SCWCD 1.4
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Originally posted by Pandu Rangam:
Hi all,
I have some text like
My Apartment, has Apartments where Apts is Apartment Aprartments @Apts. sdfasdfaApt Apt/No Aptasdf asdf Apts.. Apartments

from this i want replace all exact occurrences of
Apartment, Apartments, Apt, Apts,Apts., Apt/No with Some text, ex. "TEST".
After replacing text should look like this

My TEST, has TEST where TEST is TEST TEST @Apts. sdfasdfaApt TEST Aptasdf asdf TEST.
For this I need a regular expression.

Thanks in advance


The regular expression is quite easy: just combine all words with |:
(Apartment)|(Apartments)|(Apt)|(Apts)|(Apts\\.)|(Apt/No)

Please notice the \\. In regular expressions . means any character, so you have to escape it with \. But because that's the Java escape character as well you have to escape that as well.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Pandu Rangam
Greenhorn

Joined: Dec 08, 2006
Posts: 24
Hi Rob Prime,

The regular expression is quite easy: just combine all words with |:
(Apartment)|(Apartments)|(Apt)|(Apts)|(Apts\\.)|(Apt/No)

I did in the same way but this will replace all occurance, I mean it will replace @Apts. and sdfasdfaApt. But I don't those words to change. I wrote code like this


out put is
My TEST, has TEST where TEST is TEST Aprartments @TEST sdfasdfaTEST TEST/No TESTasdf asdf TEST. TEST

Thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
In that case you need to match a word boundary before and after the term. Have a look at "\b" for that.


Android appsImageJ pluginsJava web charts
Pandu Rangam
Greenhorn

Joined: Dec 08, 2006
Posts: 24
Hi Ulf Dittmer,
Thanks for your suggestion. I changed the code accordingly


output is My TEST, has TEST where TEST is TEST TEST @TEST. sdfasdfaApt TEST/No Aptasdf asdf TEST.. TEST

it is failing for @Apts. and Apt/No.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replace exact words using regular expressions
 
Similar Threads
difference between Ports ?
Design Exercise
(Attn: Ravish) IT salaries and living expenses in Bangalore
Array and GUI
GUI Big trouble