Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18946

Re: Regexp exception in String.replaceAll

From Steven Simpson <ss@domain.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Regexp exception in String.replaceAll
Date 2012-09-26 15:18 +0100
Organization A noiseless patient Spider
Message-ID <5efbj9-vr7.ln1@s.simpson148.btinternet.com> (permalink)
References <7ku568loa4ogik4e6asouundcbcjurg6lj@4ax.com>

Show all headers | View raw


On 26/09/12 13:57, Tim Slattery wrote:
> 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)

$ cat Replace.java
public class Replace {
     public static void main(String[] args) throws Exception {
         String upload = "oof flange {FileName} dangle";
         String fileName = "wart$5x";
         String result = upload.replaceAll("\\{FileName\\}", fileName);
         System.err.println(result);
     }
}
$ javac Replace.java
$ java Replace
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 5
         at java.util.regex.Matcher.start(Matcher.java:374)
         at java.util.regex.Matcher.appendReplacement(Matcher.java:831)
         at java.util.regex.Matcher.replaceAll(Matcher.java:906)
         at java.lang.String.replaceAll(String.java:2162)
         at Replace.main(Replace.java:5)


<http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29>:
> 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

So you probably had a "$5" in your fileName somewhere.


-- 
ss at comp dot lancs dot ac dot uk

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Regexp exception in String.replaceAll Tim Slattery <Slattery_T@bls.gov> - 2012-09-26 08:57 -0400
  Re: Regexp exception in String.replaceAll Arne Vajhøj <arne@vajhoej.dk> - 2012-09-26 09:13 -0400
  Re: Regexp exception in String.replaceAll Steven Simpson <ss@domain.invalid> - 2012-09-26 15:18 +0100
  Re: Regexp exception in String.replaceAll Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-09-26 16:30 +0200
    Re: Regexp exception in String.replaceAll Tim Slattery <Slattery_T@bls.gov> - 2012-09-26 12:35 -0400
      Re: Regexp exception in String.replaceAll markspace <-@.> - 2012-09-26 10:00 -0700
        Re: Regexp exception in String.replaceAll Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-09-26 21:26 +0200
          Re: Regexp exception in String.replaceAll Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-09-26 13:27 -0700
            Re: Regexp exception in String.replaceAll markspace <-@.> - 2012-09-26 14:31 -0700
      Re: Regexp exception in String.replaceAll Lew <lewbloch@gmail.com> - 2012-09-26 13:32 -0700
  Re: Regexp exception in String.replaceAll Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-09-26 09:43 -0700
  Re: Regexp exception in String.replaceAll Roedy Green <see_website@mindprod.com.invalid> - 2012-09-27 12:10 -0700

csiph-web