Regex String Replacement

From: Chris Nevill (Chrisinbmth_at_NothereYahoo.co.uk)
Date: 01/31/04


Date: Sat, 31 Jan 2004 01:45:23 -0000

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



Relevant Pages

  • Re: Regex String Replacement
    ... > I'm trying to do a string replace in Java for an instant messaging program. ... > If I recieve a string that ends pee! ...
    (comp.lang.java.programmer)
  • Re: performing: ups and downs
    ... I twitch like a string. ... I dance then pee. ...
    (alt.guitar)
  • Re: Tranfering unicod charcters in Socket programming!
    ... that you recieve 12 characters bytes in ... case of sending only 4 characters. ... conversion I should substitute string handling < ... I am sending unicode as string, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Tranfering unicod charcters in Socket programming!
    ... Actualy i am sending simple string, ... int SendStringAsUnicode ... To recieve a Unicode string and transform it back into MBCS, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Recieving GPS Strings with Bluetooth
    ... > I have managed to be able to read in the string from the GPS reciever, ... >>the adaptor is attached to the GPS reciever, and is sending out a string ... >>every second, my application, for some reason, will not recieve it, in fact ... >>when conencted to the bluetooth adaptor on the GPS, ...
    (microsoft.public.pocketpc.developer)