Regular expression Matcher.find error java.lang.StackOverflowError solve

Source: Internet
Author: User

A regular expression was used in the most recent Java project to crawl the contents of the Web page, but it was clearly the correct regex, but the matcher.find times was wrong:

    public static list<string>  Findstrs (STRING&NBSP;REGX,STRING&NBSP;SOURCESTR) {        pattern  Pattern = pattern.compile (REGX);        matcher m  = pattern.matcher (SOURCESTR);        list<string>  Result=new arraylist<> ();         while  (M.find () )  {            result.add (M.group (1));         }        return  Result.size () <=0?null:result;    } 

Later, one-step debugging, found that the first time to find the correct acquisition, the second error, the errors are:

Java.lang.stackoverflowerrorat java.lang.string.charat (string.java:657) at java.util.regex.pattern$ Slice.match (pattern.java:3971) at java.util.regex.pattern$lazyloop.match (pattern.java:4845) at  Java.util.regex.pattern$grouptail.match (pattern.java:4719) At java.util.regex.pattern$branchconn.match ( pattern.java:4570) At java.util.regex.pattern$charproperty.match (pattern.java:3779) at  Java.util.regex.pattern$branch.match (pattern.java:4606) At java.util.regex.pattern$grouphead.match ( pattern.java:4660) At java.util.regex.pattern$lazyloop.match (pattern.java:4849) at  Java.util.regex.pattern$grouptail.match (pattern.java:4719) At java.util.regex.pattern$branchconn.match ( pattern.java:4570) At java.util.regex.pattern$charproperty.match (pattern.java:3779) at  Java.util.regex.pattern$branch.match (pattern.java:4606) At java.util.regex.pattern$grouphead.match ( pattern.java:4660) At java.util.regex.pattern$lazyloop.match (pattern.java:4849) AT&NBSp;java.util.regex.pattern$grouptail.match (pattern.java:4719) At java.util.regex.pattern$branchconn.match ( pattern.java:4570) At java.util.regex.pattern$charproperty.match (pattern.java:3779) at  Java.util.regex.pattern$branch.match (pattern.java:4606) At java.util.regex.pattern$grouphead.match ( pattern.java:4660) At java.util.regex.pattern$lazyloop.match (pattern.java:4849) at  Java.util.regex.pattern$grouptail.match (pattern.java:4719) At java.util.regex.pattern$branchconn.match ( pattern.java:4570) At java.util.regex.pattern$charproperty.match (pattern.java:3779) at  Java.util.regex.pattern$branch.match (pattern.java:4606) At java.util.regex.pattern$grouphead.match ( pattern.java:4660) At java.util.regex.pattern$lazyloop.match (pattern.java:4849) at  Java.util.regex.pattern$grouptail.match (pattern.java:4719) At java.util.regex.pattern$branchconn.match ( pattern.java:4570) At java.util.regex.pattern$charproperty.match (pattern.java:3779) At javA.util.regex.pattern$branch.match (pattern.java:4606) At java.util.regex.pattern$grouphead.match ( pattern.java:4660) At java.util.regex.pattern$lazyloop.match (pattern.java:4849) ...  

In the online search, found that the problem is a stack overflow, someone gave a solution:

Pattern pattern = pattern.compile (Re,pattern.dotall + pattern.multiline);

where Pattern.dotall + Pattern.multiline refers to: in a regular expression. You can replace all characters, including the line break \ n. The original wording may be: xxx (?: \ n|.) *?xxx, now can be written xxx.*?xxx,.

Using this method, the problem is temporarily resolved. But the problem is not clear, it may be the SDK bug it.

Note: This method avoids this problem, but comes with, because when you use., the wrapping is ignored, and the regular expression may be used to match to a larger range of content, so you need to be cautious when writing the regular. In addition, later I tried, as long as the match less than two line-wrapping content, do not use this method, also can run normally, so feel in Java to use regular expression, try to match a small amount of content, there will be no error.


Regular expression Matcher.find error java.lang.StackOverflowError solve

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.