Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: Regexp exception in String.replaceAll Date: Wed, 26 Sep 2012 16:30:42 +0200 Organization: A noiseless patient Spider Lines: 32 Message-ID: References: <7ku568loa4ogik4e6asouundcbcjurg6lj@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Wed, 26 Sep 2012 14:30:36 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="ab0f4a36851f1e27a3b9bb29e0bb4e7c"; logging-data="9828"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mByD1/F52papRn2QngrRU" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20 In-Reply-To: <7ku568loa4ogik4e6asouundcbcjurg6lj@4ax.com> Cancel-Lock: sha1:YRv0jbZncws1BwvQwbWzHCsCA/Q= Xref: csiph.com comp.lang.java.programmer:18947 On 26/09/2012 14:57, Tim Slattery allegedly wrote: > I'm getting an "IndexOutOfBoundsException" from the regex processor > when I call String.replaceAll. > > Here's the call: > > String result = upload.replaceAll("\\{FileName\\}", fileName); > > "upload" is a string that contains at least one occurrence of > "{FileName}", which is to be replaced by the actual filename passed > into this method. Unfortunately, I don't know what the filename was > when the exception occurred. The stacktrace looks like this: > > java.lang.IndexOutOfBoundsException: No group 5 > at java.util.regex.Matcher.group(Matcher.java:470) > at java.util.regex.Matcher.appendReplacement(Matcher.java:737) > at java.util.regex.Matcher.replaceAll(Matcher.java:813) > at java.lang.String.replaceAll(String.java:2189) > at (the line shown above) > > I'm aware that replaceAll uses the regexp mechanism for matching > strings. But I can't imagine what could cause this exception. Does > anybody have a clue? > Read the Javadoc carefully: > Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired. -- DF.