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
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