Regex String Replacement

C

Chris Nevill

Hi
I'm trying to do a string replace in Java for an instant messaging program.
This is for an assignment I'm working on.
using .replaceAll
The problem I have is this
I need to filter say a certain word in a line of text
recieved from the user for the instant messaging program.
Lets presume the word I have is pee
I must replace pee with asterisks ***
However if I recieve a string that contains speed I must not filter it
If I recieve a string that ends pee! or pee. etc
I must filter it.

so far the closest I've got is this:-
String RudeWord = "pee"
Pattern pattern = Pattern.compile("(^|[^\\w])("+rudeWord+")([^\\w]|$)");
Matcher matcher = pattern.matcher(filteredMessage);
filteredMessage=matcher.replaceAll(" " + rudeWord.replaceAll(".","*")+"
");

To start with this looked good, until a friend pointed out that if a string
is recieved
pee! it changes it to *** not ***!
and so on!

Any ideas would be much appreciated!
Cheers
Chris
 
H

hiwa

Chris Nevill said:
Hi
I'm trying to do a string replace in Java for an instant messaging program.
This is for an assignment I'm working on.
using .replaceAll
The problem I have is this
I need to filter say a certain word in a line of text
recieved from the user for the instant messaging program.
Lets presume the word I have is pee
I must replace pee with asterisks ***
However if I recieve a string that contains speed I must not filter it
If I recieve a string that ends pee! or pee. etc
I must filter it.

so far the closest I've got is this:-
String RudeWord = "pee"
Pattern pattern = Pattern.compile("(^|[^\\w])("+rudeWord+")([^\\w]|$)");
Matcher matcher = pattern.matcher(filteredMessage);
filteredMessage=matcher.replaceAll(" " + rudeWord.replaceAll(".","*")+"
");

To start with this looked good, until a friend pointed out that if a string
is recieved
pee! it changes it to *** not ***!
and so on!

Any ideas would be much appreciated!
Cheers
Chris
Code:
String censoredResult = originalString.replaceAll
("(\\W)(pee)(\\W)", "$1***$3");
 
H

hiwa

Sorry. There's an error in my previous post.
The third matching group shoud be (\\W|$) instead of just (\\W).
 
H

hiwa

Sorry again. The correct regexp is as follows:
"(\\b)(pee)(\\b)"

Try this and other regexps for "pee pee! is mine spee pee pee!"
or "pee pee are just pee pee"
 
T

Thomas Schodt

hiwa said:
Sorry again. The correct regexp is as follows:
"(\\b)(pee)(\\b)"

Pretty much what I would do except "\b" is more like an assertion
and as such does not match any characters.

For testing purposes:

<code>
class Censor {
public static void main(String[] argv) {
System.out.println(argv[0].replaceAll(
"\\b(pee|poo)\\b", "***"));
}
}
</code>

java Censor "pee speed spoon poo"
 
C

Chris Nevill

Thats great cheers!
Chris
----- Original Message -----
From: "hiwa" <[email protected]>
Newsgroups: comp.lang.java.programmer
Sent: 31 January, 2004 10:33 AM
Subject: Re: Regex String Replacement
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top