4 Replies - 3394 Views - Last Post: 06 June 2012 - 07:57 PM Rate Topic: -----

#1 MattyDanielle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-March 12

Unclosed group near index Error when using String.split

Posted 06 June 2012 - 05:35 PM

I have the following line of code giving me an error:
piece = piece.split(after[x])[0];


The error is :
java.util.regex.PatternSyntaxException: Unclosed group near index 190
" coords="466,79,13" href="#g30" alt="" onmouseover="return overlib('<b>New Brighton Park</b><br>Windemere, Nootka & Waterfront' , SNAPX, 20, SNAPY, 20);" onmouseout="return nd(


There is an arrow pointing up at the last bracket in the string. I have never gotten this error before but other forum posts suggest using .replaceAll("(", "") but that does not work for me. I just get basically the same error just at index 1 instead of 190. Can anyone suggest a way to fix this? I would really appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Unclosed group near index Error when using String.split

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Unclosed group near index Error when using String.split

Posted 06 June 2012 - 07:32 PM

Note that String.split expects a regular expression. So that long string you've sent will be interpreted as a one, and the opening and closing parens are treated as meta-characters.

Exception re-creation
Solution

You'll probably want to write a method to escape all meta-characters.
Was This Post Helpful? 1
  • +
  • -

#3 MattyDanielle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-March 12

Re: Unclosed group near index Error when using String.split

Posted 06 June 2012 - 07:35 PM

Oh my. Thank you! And wow that site is cool. Thanks for showing me a solution as well!
Was This Post Helpful? 0
  • +
  • -

#4 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Unclosed group near index Error when using String.split

Posted 06 June 2012 - 07:38 PM

"(" is a special character in regex so you need to escape it, try:
replaceAll("\\(", "");

This will remove all the "(" form your strings which might not be what you want because then if you try to split "sauce" on "(u" it will split it on "u" since you remove "(".
If you want to take that into consideration you could try:
piece.split("\\Q" + after[x] + "\\E")[0];

now it will not split at all if you try to split "sauce" on "(u", but it will not cause errors at runtime.
NB: None of these work correctly if "after[x]" is a regex itself, instead of a plain quote/text.
Was This Post Helpful? 1
  • +
  • -

#5 MattyDanielle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-March 12

Re: Unclosed group near index Error when using String.split

Posted 06 June 2012 - 07:57 PM

Thanks guys! Also found this method on google:

StringEscapeUtils.escapeHtml(String);


from

org.apache.commons.lang.StringEscapeUtils

Now my code is working perfect.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1