Skip to content Skip to sidebar Skip to footer

Matcher Don't Find On 2nd Loop

1st step is everyth ok, but on 2nd step goes someth wrong: it is very slow (20+ sec) on find(). Besides matches() always returs false, and I don't know why. Separately every regex

Solution 1:

If you really need to reuse the String derived from Matcher.group, better create a new String since what you get from Matcher.group is actually still a substring whose reference pointing to the original one. Thus when you second time read the reference and pass to Pattern.matcher(), you are not actually pass the exact String you get from the first step. Maybe it's a bug or it's designed to work this way. But for your situation, create a new String each time for Matcher.group() always makes life easier. Hope I explained it correctly and completely.

String[] regexContent = {"node[\\s\\S]*?(<p>[\\s\\S]+</p>)", 
"([\\s\\S]*?)</div>"};

Pattern p;
Matcher m;


    for (String regex : regexContent){ 

        p = Pattern.compile(regex);
        m = p.matcher(result);

        //if (m.matches()) // always false
        result = "";
        if (m.find()) // on 2nd step waits for so long time & don't find
            result = new String(m.group(m.groupCount()));
        m.reset();

    }

Post a Comment for "Matcher Don't Find On 2nd Loop"