Results 1 to 12 of 12
  1. #1
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default Preventing stack overflow?

    Hi,
    Is there a way to replace an old variable value with a new one? I'm using a really long string and using a loop to replace all instances of some pattern. There are a lot of these find and replace loops, which is why there is a stack overflow error.

    Example of one:
    Java Code:
    String content = /*some long string*/;
    Pattern nbspPattern = Pattern.compile("(?i) ");
    Matcher nbspMatcher = nbspPattern.matcher(content);
    	while(nbspMatcher.find()){
    		content = content.replaceAll("(?i) "," ");
    	}
    Is there a way to clear the content value before adding the new edited string, or would multiple variables be better - like for the other patterns, create new strings called content2, content3, etc (or arrays)?

    I'm open to any other suggestions as well, such as alternatives or more efficient ways of accomplishing the above.

    Thanks

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    If you are using replaceAll(), then no need to use a while loop. As the name of the method goes, it replaces all the patterns found.

    also , the String class has a replaceAll() method, so there's no need to use regex library.
    Java Code:
    String content = /*some long string*/;
    conten = content.replaceAll( "(?i) ", "");

  3. #3
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    Ok cool. That's how it was at first, until the stack overflow error then I just started messing with the code any way I knew how.

    However, the error prevents the script form doing anything useful.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    How long is the parent String?
    Can you break that string up in a meaningful way?

  5. #5
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    I've been reading up on stack overflow errors, and a lot of user explanations on other websites formed it as loops that just append to a string. However, the code I'm using, replaces the string which is not a problem. The actual error comes from one of the regex patterns.

    Java Code:
    String content = /*some long string*/;
    content = content.replaceAll( "/*(.*?)*/", "");
    I'm trying to get the function to find the actual characters /* but it looks as though it's interpreting the * as its special meaning. The code is running through eclipse, but whenever I insert an escape character \ an error shows up saying that it's not a valid escape sequence, even though every website out there says to do so. Maybe theres a fundamental import or snytax that I have not learned, but what I'm hoping the function will do is find the actual characters /* at the beginning, and */ at the end, with any kind of characters in between.

    So the question is, what is the proper syntax to find the actual characters /* in a string?

    Thanks

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    If you need to escape those characters (and my regex is rubbish by the way), you can't use a single '\', since that would tell Java to escape the next character itself (not regex), and '/' is not a legitimate character to escape...so you need to escape the escape..."\\".

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaHater View Post
    you escape like this
    Java Code:
     "\\/\\*"
    There's no need to escape a forward slash, it isn't a special character; read the API documentation for the Pattern class; it explains the syntax of regular expressions.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    That's good to know. I'll probably be learning that in the coming years.
    I've been reading the tutorials only, but the documentations seem to help a lot.

    Thanks for your help.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaHater View Post
    In Java, maybe. But if one use is in on a *nix system for example, the forward slash is often a special character for tools such as awk/sed or Perl etc.(and of course, the shell)

    In Java, its doesn't matter anyway to escape it.

    And by the way, there is no need to read the regex API for me to know my regular expression syntax. Its been 10+ years since i started playing with it.
    Read the top left of this page: guess what? this is a Java forum. So you are a freshman. Regular expressions where already with us from the mid seventies.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    Read the top left of this page: guess what? this is a Java forum. So you are a freshman. Regular expressions where already with us from the mid seventies.

    Jos
    At least I got my excuse in from the start...:)

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    At least I got my excuse in from the start...:)
    Same here: I fell on my head when I was a baby so I'm always innocent.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Several posts irrelevant to the original post and at least one post that gave erronious information deleted. Thread locked as it is straying from the original question.

Similar Threads

  1. Preventing exceptions
    By ieoj31 in forum New To Java
    Replies: 3
    Last Post: 11-15-2010, 02:33 PM
  2. Replies: 1
    Last Post: 03-17-2010, 06:25 AM
  3. Stack Overflow work around?
    By Coukapecker in forum New To Java
    Replies: 2
    Last Post: 03-14-2010, 09:49 PM
  4. Java Stack Overflow?
    By fullmetaljacket in forum New To Java
    Replies: 0
    Last Post: 05-19-2009, 08:49 PM
  5. Graphics2D: stack overflow error
    By rosh72851 in forum New To Java
    Replies: 11
    Last Post: 10-15-2008, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •