My goal is to replace any series of spaces (ie. " ", " ", " ", and so on) to this: " =$A$"
Here's my code to pull that off:
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Space
{
public static void main( String args[] )
{
Pattern spacePattern = Pattern.compile( "[ ]+" );
String exchange = " =$AS";
String output = spacePattern.matcher( " 1 2 3 4" ).replaceAll( exchange );
System.out.println( output );
}
}
It compiles just fine, but upon execution, you get this:
Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(Matcher.java:561)
at java.util.regex.Matcher.replaceAll(Matcher.java:661)
at Space.main(Space.java:11)
It appears the '$' in the replaceAll argument is the culprit but I need it for my work. Any help?
2006-09-09
12:12:57
·
5 answers
·
asked by
Scott
1