Forums Register Login

Regex Issue

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
Thanks Alan!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 839 times.
Similar Threads
Netscape7.1 and window.createPopup()
Why we live?
input type=file
onload
build.xml question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:01:33.