aspose 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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 354
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 Mock exam / preparation kit reviews
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex Issue
 
Similar Threads
Why we live?
build.xml question
input type=file
Netscape7.1 and window.createPopup()
onload