Regex String Replacement
From: Chris Nevill (Chrisinbmth_at_NothereYahoo.co.uk)
Date: 01/31/04
- Next message: sarah chang: "Re: Trying to install Apache AXIS on WAS 4 AE"
- Previous message: Wolfgang: "Re: Inexplicable behavior of paint(Graphics g)"
- Next in thread: hiwa: "Re: Regex String Replacement"
- Reply: hiwa: "Re: Regex String Replacement"
- Reply: hiwa: "Re: Regex String Replacement"
- Reply: hiwa: "Re: Regex String Replacement"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: sarah chang: "Re: Trying to install Apache AXIS on WAS 4 AE"
- Previous message: Wolfgang: "Re: Inexplicable behavior of paint(Graphics g)"
- Next in thread: hiwa: "Re: Regex String Replacement"
- Reply: hiwa: "Re: Regex String Replacement"
- Reply: hiwa: "Re: Regex String Replacement"
- Reply: hiwa: "Re: Regex String Replacement"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|