Results 1 to 7 of 7
  1. #1
    masterrs.mind is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Exclamation StackOverflow Exception with Regexes

    Hi All,

    I am using Regular Expressions in order to replace and strip certain html tags from the body of html.
    I am using a properties file with key value pairs for the Regex patterns
    I am encountering the following errors when each time the Regular Expression has been executed.

    Exception in thread "main" java.lang.StackOverflowError
    at java.util.regex.Pattern$LazyLoop.match(Unknown Source)
    at java.util.regex.Pattern$GroupTail.match(Unknown Source)
    at java.util.regex.Pattern$BranchConn.match(Unknown Source)
    at java.util.regex.Pattern$CharProperty.match(Unknown Source)
    at java.util.regex.Pattern$Branch.match(Unknown Source)
    at java.util.regex.Pattern$GroupHead.match(Unknown Source)

    Here are the properties.
    0 T:\\(.*?\\): :
    1 R:<P>&nbsp;</P>: :
    2 R:&nbsp;: :
    3 R:<SPAN(.|\n)*?>: :
    4 R:</SPAN>: :
    5 R:<TD(.|\n)*?>:<td>:
    6 R:<P><IMG(.|\n)*?></P>:<hr/>:
    7 C:StrongPattern:<P>(.|\n)*?<STRONG>(.|\n)*?/P>:<h1>:</h1>:<h2>:</h2>:
    8 C:NestedTablePattern:(?s)<td>(.*?)</td>:<h1>:</h1>:
    9 R:<p><br/>[\\r\\n]+<h1>:<h1>:
    10 R:</h1></p>:</h1>:
    11 R:<P><IMG(.|\n)*?>:<hr/>:

    Is it something wrong with my Regexes?How can I possibly eliminate the errors.

    Thank in advance.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    StackOverflowError means that you have infinite recursion thing happening in your code. Did you check anything for that in your code?

    Is that the complete error message you comes with?

  3. #3
    masterrs.mind is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default

    Hi,
    yeah I got it resolved,I changed my regex definition and now it is working fine.
    Yes that is due to infinite recursion.

    Thanks

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    It may not be infinite, but just too much. Regex can do crazy things, even if they are valid.

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Maybe you could post the changes you made?

  6. #6
    masterrs.mind is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default

    Actually I was getting the error when running transformations for IMG tag and <P><STRONG> patterns,because i don't need to check for next lines in those cases(previously they were <P><IMG(.|\n)*?></P>)

    All I have done is removing |(or) from Regexps.
    So my Regexps turned out to be

    6 R:<P><IMG(.)*?></P>:<HR/>:
    7 R:<IMG(.)*?>: :
    8 C:StrongPattern:<P>(.)*?<STRONG>(.)*?/P>:<H1>:</H1>:<H2>:</H2>:

    That solved my problem.
    Hope this will be helpful for someone.

  7. #7
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default Other simplifications

    In general, it may well be more efficient to use character classes [] instead of the pipe where you can achieve the equivalent. So you could write:

    <P><IMG[.\n]*?></P>:<hr/>

    You can also set the DOT_ALL flag when you compile the pattern instead of putting both dot and newline (unless you really want to include, say, \n but not \r, I guess).

    I personally think it's a bit odd to have a capturing group modified by a star (....)* (perhaps you meant a non-capturing group (?:.....)*.

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  2. How can I fix this exception?
    By Abder-Rahman in forum Java 2D
    Replies: 1
    Last Post: 12-21-2009, 02:04 AM
  3. Exception
    By sreejit in forum Advanced Java
    Replies: 2
    Last Post: 10-21-2009, 08:26 AM
  4. Need Help with exception
    By maggie_2 in forum New To Java
    Replies: 7
    Last Post: 11-26-2008, 11:45 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 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
  •