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


Groups > comp.lang.java.programmer > #18942 > unrolled thread

Regexp exception in String.replaceAll

Started byTim Slattery <Slattery_T@bls.gov>
First post2012-09-26 08:57 -0400
Last post2012-09-27 12:10 -0700
Articles 12 — 8 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#18942 — Regexp exception in String.replaceAll

FromTim Slattery <Slattery_T@bls.gov>
Date2012-09-26 08:57 -0400
SubjectRegexp exception in String.replaceAll
Message-ID<7ku568loa4ogik4e6asouundcbcjurg6lj@4ax.com>
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?

-- 
Tim Slattery
Slattery_T@bls.gov

[toc] | [next] | [standalone]


#18943

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-09-26 09:13 -0400
Message-ID<5062ff6b$0$295$14726298@news.sunsite.dk>
In reply to#18942
On 9/26/2012 8:57 AM, Tim Slattery 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?

There are no reason why that replaceAll should try and access group 5.

I suspect the exception comes from a different line.

Arne

[toc] | [prev] | [next] | [standalone]


#18946

FromSteven Simpson <ss@domain.invalid>
Date2012-09-26 15:18 +0100
Message-ID<5efbj9-vr7.ln1@s.simpson148.btinternet.com>
In reply to#18942
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

[toc] | [prev] | [next] | [standalone]


#18947

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2012-09-26 16:30 +0200
Message-ID<k3v3ic$9j4$1@dont-email.me>
In reply to#18942
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:

<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; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired. 

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#18952

FromTim Slattery <Slattery_T@bls.gov>
Date2012-09-26 12:35 -0400
Message-ID<u0b668hvravflnt1upsks1rv0bnsmlt6us@4ax.com>
In reply to#18947
Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> wrote:


>Read the Javadoc carefully:
>
><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; see Matcher.replaceAll. Use 
>> Matcher.quoteReplacement(java.lang.String) to suppress the special 
>> meaning of these characters, if desired. 

Yes, I had completely missed that. I thought it was a literal
replacement string.

In JSE4 and 5 the "replace" method took arguments of type "char". Now
I see that in JSE6 those arguments are CharSequence, an interface
which String implements. So before JSE6, if you needed String
arguments, you had a choice of ReplaceAll or ReplaceFirst. Since I
needed to allow for multiple occurrences of the replace string, I had
to use ReplaceAll, and I missed this trap. (And I never considered
that anybody would use a dollar sign in a filename!). Now that we're
using JSE6, I can just change it to call replace instead of
replaceAll.

-- 
Tim Slattery
Slattery_T@bls.gov

[toc] | [prev] | [next] | [standalone]


#18954

Frommarkspace <-@.>
Date2012-09-26 10:00 -0700
Message-ID<k3vcan$67i$1@dont-email.me>
In reply to#18952
On 9/26/2012 9:35 AM, Tim Slattery wrote:
> So before JSE6, if you needed String
> arguments, you had a choice of ReplaceAll or ReplaceFirst. Since I
> needed to allow for multiple occurrences of the replace string, I had
> to use ReplaceAll, and I missed this trap. (And I never considered
> that anybody would use a dollar sign in a filename!). Now that we're
> using JSE6, I can just change it to call replace instead of
> replaceAll.


Just to be a little pendantic here, I'm pretty sure that the "new" 
String::replace just calls Pattern.quote before calling 
String.replaceAll/First.  Pattern.quote has been around since Java 1.5, 
so it's not really new.

<http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#quote%28java.lang.String%29>

[toc] | [prev] | [next] | [standalone]


#18955

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2012-09-26 21:26 +0200
Message-ID<k3vktg$1a2$1@dont-email.me>
In reply to#18954
On 26/09/2012 19:00, markspace allegedly wrote:
> On 9/26/2012 9:35 AM, Tim Slattery wrote:
>> So before JSE6, if you needed String
>> arguments, you had a choice of ReplaceAll or ReplaceFirst. Since I
>> needed to allow for multiple occurrences of the replace string, I had
>> to use ReplaceAll, and I missed this trap. (And I never considered
>> that anybody would use a dollar sign in a filename!). Now that we're
>> using JSE6, I can just change it to call replace instead of
>> replaceAll.
> 
> 
> Just to be a little pendantic here, I'm pretty sure that the "new"
> String::replace just calls Pattern.quote before calling
> String.replaceAll/First.  Pattern.quote has been around since Java 1.5,
> so it's not really new.
> 
> <http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#quote%28java.lang.String%29>

To be a little pedantic myself and for the record, I don't think the
quoting of a Pattern and the quoting of a replacement string are the
same type of quoting.

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#18957

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-09-26 13:27 -0700
Message-ID<syJ8s.3$g24.0@newsfe23.iad>
In reply to#18955
On 9/26/12 12:26 PM, Daniele Futtorovic wrote:
> On 26/09/2012 19:00, markspace allegedly wrote:
>> On 9/26/2012 9:35 AM, Tim Slattery wrote:
>>> So before JSE6, if you needed String
>>> arguments, you had a choice of ReplaceAll or ReplaceFirst. Since I
>>> needed to allow for multiple occurrences of the replace string, I had
>>> to use ReplaceAll, and I missed this trap. (And I never considered
>>> that anybody would use a dollar sign in a filename!). Now that we're
>>> using JSE6, I can just change it to call replace instead of
>>> replaceAll.
>>
>>
>> Just to be a little pendantic here, I'm pretty sure that the "new"
>> String::replace just calls Pattern.quote before calling
>> String.replaceAll/First.  Pattern.quote has been around since Java 1.5,
>> so it's not really new.
>>
>> <http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#quote%28java.lang.String%29>
>
> To be a little pedantic myself and for the record, I don't think the
> quoting of a Pattern and the quoting of a replacement string are the
> same type of quoting.
>
Indeed, Pattern.quote is for quoting the pattern, and 
Matcher.quoteReplacement is for quoting the replacement.  The OP should 
be using both.

[toc] | [prev] | [next] | [standalone]


#18959

Frommarkspace <-@.>
Date2012-09-26 14:31 -0700
Message-ID<k3vs7p$jjd$1@dont-email.me>
In reply to#18957
On 9/26/2012 1:27 PM, Daniel Pitts wrote:

> On 9/26/12 12:26 PM, Daniele Futtorovic wrote:
>> To be a little pedantic myself and for the record, I don't think the
>> quoting of a Pattern and the quoting of a replacement string are the
>> same type of quoting.

> Indeed, Pattern.quote is for quoting the pattern, and
> Matcher.quoteReplacement is for quoting the replacement.  The OP should
> be using both.


Nope that's what I get for not paying close attention to what's being 
discussed.  Correct on both counts!

[toc] | [prev] | [next] | [standalone]


#18958

FromLew <lewbloch@gmail.com>
Date2012-09-26 13:32 -0700
Message-ID<ee3feb2a-b390-4828-a2a2-fb5be29c5856@googlegroups.com>
In reply to#18952
Tim Slattery wrote:
> Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> wrote:
>>Read the Javadoc carefully:
> 
> Yes, I had completely missed that. I thought it was a literal
> replacement string.
> 
> In JSE4 [sic] and 5 the "replace" method took arguments of type "char". Now

Still does in Java 6 and 7.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(char, char)
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace(char, char)

> I see that in JSE6 those arguments are CharSequence, an interface

Same as in Java 5.
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#replace(java.lang.CharSequence, java.lang.CharSequence)

> which String implements. So before JSE6, if you needed String
> arguments, you had a choice of ReplaceAll [sic] or ReplaceFirst [sic]. Since I

The behavior of 'String#replaceAll()' has not changed. It always took regexes.
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)

> needed to allow for multiple occurrences of the replace string, I had
> to use ReplaceAll [sic], and I missed this trap. (And I never considered

It hasn't changed, ever.

> that anybody would use a dollar sign in a filename!). Now that we're

Really?

It's quite routine, and Java programmers should find it especially common.

> using JSE6, I can just change it to call replace instead of
> replaceAll.

Since those methods work *exactly* the same as in Java 5, I'm not sure
what you're telling us here.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#18953

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-09-26 09:43 -0700
Message-ID<QgG8s.5177$k62.2526@newsfe05.iad>
In reply to#18942
On 9/26/12 5:57 AM, Tim Slattery 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?
>
if the fileName variable contains a '\' followed by a '5' perhaps?

You should be escaping both the replacement and the search term.

See Matcher.quoteReplacement(String) and Pattern.quote(String):

<http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#quoteReplacement(java.lang.String)>

<http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#quote(java.lang.String)>

[toc] | [prev] | [next] | [standalone]


#18963

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-09-27 12:10 -0700
Message-ID<3v89681ugms051o2kr4n040ehj6rkkq16l@4ax.com>
In reply to#18942
On Wed, 26 Sep 2012 08:57:53 -0400, Tim Slattery <Slattery_T@bls.gov>
wrote, quoted or indirectly quoted someone who said :

>String result = upload.replaceAll("\\{FileName\\}", fileName);

In the Javadoc for replaceAll  is warns you:
 Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
the replacement string may cause the results to be different than if
it were  being treated as a literal replacement string; see
     {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}.
     Use {@link java.util.regex.Matcher#quoteReplacement} to suppress
the special meaning of these characters, if desired.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web