how to replace all the stoping words?
Hi Guys!
I need your guy抯 suggestion. I am writing a code using regex. My motive it so replace the entire stopping words with 憇pace character? I use pattern and matchers. I wrote a piece of code, which works fine. But my program works for just one words. Wherever I find 慶at?I replace it with 慸og? Now let say if I have a big String or may be a file and I want to replace all the stopping words in it. Do I have to create a whole bunch of patterns and matcher to replace the words? OR is there any other way to do it? Like most of the things here is a catch, I don抰 want to use String because of speed.
Here is my code
Pattern p = Pattern.compile("cat",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("one cat two CATs in the yard");
StringBuffer sb =new StringBuffer();
while (m.find()){
m.appendReplacement(sb,"dog");
}
m.appendTail(sb);
System.out.println(sb.toString());
Thanks alot in Advance
[1175 byte] By [
asyed01a] at [2007-11-27 6:08:23]

import e.util.*;
import java.util.*;
import java.util.regex.*;
/**
* For Rewriter see http://elliotth.blogspot.com/2004/07/java-implementation-of-rubys-gsub.html
*/
class Replacer extends Rewriter
{
public Replacer()
{
super("(?i)(cat|pig)");
}
public String replacement()
{
final String newValue = replacements_.get(group(1));
return Matcher.quoteReplacement((newValue == null) ? group(0) : newValue);
}
public void putReplacement(String key, String value)
{
replacements_.put(key, value);
}
private final Map<String, String> replacements_ = new HashMap<String, String>();
}
public class Fred906_1
{
public static void main(String[] args) throws Exception
{
String fileContent = "The quick brown cat jumps over the lazy pig.";
Replacer macroProcessor = new Replacer();
macroProcessor.putReplacement("cat","fox");
macroProcessor.putReplacement("pig","dog");
System.out.println(macroProcessor.rewrite(fileContent));
}
}
Who knows or cares unless speed turns out to be a problem. Suck it and see.
In my experience, simple RE's (those without look-aheads and look-behinds) are faster than there equivalent series of raw string comparisons... simply because the guys who write RE engines are smarter than me :-)
Keith.