File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Regex Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regex Issue" Watch "Regex Issue" New topic
Author

Regex Issue

Luke Shannon
Ranch Hand

Joined: Sep 30, 2004
Posts: 239
We have this line of code:



If current.getDescripton() returns a string that contains a $ characher I get the following error:

java.lang.IndexOutOfBoundsException: No group 2

How can this be avoided?

Luke


Luke
SCJP
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
If you're using JDK 1.5, you can use Matcher.quoteReplacement(String) to escape the special characters in the replacement string (i.e., dollar sign and backslash). Otherwise, you can do it yourself:
Luke Shannon
Ranch Hand

Joined: Sep 30, 2004
Posts: 239
Thanks Alan!
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 356
hello,

prior to 1.5 the best way to quote a String in such a situation was to put it in \Q and \E, like that:
"\\Q{...}\\E"

Conan


Kai Witte's business website Kai Witte's private homepage
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Just to clarify, Conan, what you're talking about is quoting the regex, which is the first argument to replaceAll(). It doesn't work for the second argument, the replacement string. As of JDK 1.5, you can use the Pattern.quote() method to quote the regex, and Matcher.quoteReplacement() to quote the replacement string.
 
Don't get me started about those stupid light bulbs.
 
subject: Regex Issue
 
Similar Threads
Netscape7.1 and window.createPopup()
Why we live?
onload
input type=file
build.xml question