Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

String.replaceFirst - Illegal group reference

72 views
Skip to first unread message

O.B.

unread,
Dec 6, 2006, 11:06:45 PM12/6/06
to
In Java 1.4, the following works fine:

String keystorePath = "C:/ims/jboss/test/me";
String jbossPath = "C:/ims/jboss";
keystorePath.replaceFirst(jbossPath, "${jboss.server.home.dir}");

In Java 5.0, I get the following error:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal
group reference
at java.util.regex.Matcher.appendReplacement(Matcher.java:706)
at java.util.regex.Matcher.replaceFirst(Matcher.java:848)
at java.lang.String.replaceFirst(String.java:1967)
at TestClass.main(TestClass.java:583)

Help?

hiwa

unread,
Dec 6, 2006, 11:16:41 PM12/6/06
to
'$' is a special character for Java regex. You should escape it with
'\\'.

EJP

unread,
Dec 7, 2006, 12:51:20 AM12/7/06
to
hiwa wrote:
> '$' is a special character for Java regex.

Almost, not quite. '$' is a special character in the *replacement
string*. 'Each occurrence of $g will be replaced by the result of
evaluating group(g)'. See
http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html#appendReplacement(java.lang.StringBuffer,%20java.lang.String)

> You should escape it with
> '\\'.

You should escape it as '\$'.

hiwa

unread,
Dec 7, 2006, 3:18:56 AM12/7/06
to
Let me rephrase:
'$' is a special character for Java regex engine.
You should escape it as "\\$" in a Java string.

0 new messages