aspose file tools
The moose likes Java in General and the fly likes Pattern ReplaceAll issue (Illegal Group exception) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Pattern ReplaceAll issue (Illegal Group exception)" Watch "Pattern ReplaceAll issue (Illegal Group exception)" New topic
Author

Pattern ReplaceAll issue (Illegal Group exception)

Tony Smith
Ranch Hand

Joined: Mar 15, 2005
Posts: 77
Fiddling with regular expressions...

This line:

aMatcher.replaceAll(value);

is throwing the following mess, pasted below.

The crux is that value contains a "$". What is the proper way to escape this? I tried preceeding it with


(which is funny) but no dice.


didn't seem to help either. Ideas?

-=-=-=-=-=-

java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(Unknown Source)
at java.util.regex.Matcher.replaceAll(Unknown Source)
at com.comdata.svs.util.SkeletonRplc.subsLine(SkeletonRplc.java:158)
at com.comdata.svs.util.SkeletonRplcTest.testSubsLine6(SkeletonRplcTest.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Tony Smith
Ranch Hand

Joined: Mar 15, 2005
Posts: 77
Ok, after thinking about it, it seems one issue is that I improperly escaped the string.

Instead of


I think I really need


So that the end result actually has the backslashes embedded within it.

But this is stinky. Is there a more elegant way?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Tony Smith:
Ok, after thinking about it, it seems one issue is that I improperly escaped the string.

Instead of


I think I really need


So that the end result actually has the backslashes embedded within it.

But this is stinky. Is there a more elegant way?

No, I don't think there is a more elegant way of escaping (pun not intended) Escape Character Hell (TM). Well, you could read the regex from a file which will presumably negate the need for some of the escape characters. The solution you found actually makes sense if you think through all the stages where escape characters are required (i.e. the Java compiler and then the regex engine).

Layne


Java API Documentation
The Java Tutorial
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
If you're running Java 5, you can use Matcher's new quoteReplacement method. If you're rolling your own, you should probably escape backslashes as well as dollar signs:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
I believe Matcher.quoteReplacement() is for escaping the replacement string (the second argument in replaceAll()). To escape the target string (the first argument), use Pattern.quote() instead. Or you can use the new replace(CharSequence, CharSequence) in String, which treats both arguments literally (I think).

Ugh. It's nice that they finally provided these methods, but putting them arbitrarily in different classes like that -ugh. And I don't know how anyone is supposed to guess that replaceAll() uses regexes, but replace() does not. Very annoying.


"I'm not back." - Bill Harding, Twister
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
The replacement string is what Tony was talking about--second argument to String#replaceAll, first (only) argument to Matcher#replaceAll.

The biggest problem I have with the API is the fact that the docs for String#replaceAll and String#replaceFirst don't even mention that there's anything special about the second argument. You have to follow a link to the corresponding Matcher methods just to learn that some characters have special meaning, and if you want to know how they actually work you have to follow another link, to the appendReplacement method. It's almost like they're trying to keep it secret.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Pattern ReplaceAll issue (Illegal Group exception)
 
Similar Threads
Jboss rar JNDI lookup
JUnit on eclipse
No operation name specified - How to Look For Where the Problem is?
Unit testing portlets
expected:5 but was:5.0000000