匹配没有找到第二循环 [英] Matcher don't find on 2nd loop

查看:132
本文介绍了匹配没有找到第二循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一步是everyth好的,但在第2步去someth错了:这是很慢的(20+秒)的找到()。除了匹配()总是returs ,我不知道为什么。另外每一个正则表达式工作正常。使用仿真器。球员Thax公司

 的String [] regexContent = {节点[\\\\小号\\\\ S] *?(小于P> [\\\\小号\\\\ S] + LT; / P>) 
(?[\\\\ \\\\小号S] *)< / DIV>中};    模式P;
    匹配器米;
        对于(字符串正则表达式:regexContent){            P = Pattern.compile(正则表达式);
            M = p.matcher(结果);            //如果(m.matches())//始终为假
            结果=;
            如果(m.find())//了这么长的时间和放第2步等待;没有发现
                结果= m.group(m.groupCount());
            m.reset();        }


解决方案

如果你真的需要重用Matcher.group得到的字符串,更好地创造一个新的String,因为你从Matcher.group得到什么实际上仍然是一个字符串,其引用指向原始。因此,当你第二次阅读参考,并传递给Pattern.matcher(),你实际上并没有通过你从第一步得到确切的字符串。也许这是一个错误或它的设计以这种方式工作。但是,对于您的情况,创建一个新的String为Matcher.group每次()总是让生活更轻松。希望我正确解释完全是。

 的String [] regexContent = {节点[\\\\小号\\\\ S] *?(小于P> [\\\\小号\\\\ S] + LT; / P>) 
(?[\\\\ \\\\小号S] *)< / DIV>中};模式P;
匹配器米;
    对于(字符串正则表达式:regexContent){        P = Pattern.compile(正则表达式);
        M = p.matcher(结果);        //如果(m.matches())//始终为假
        结果=;
        如果(m.find())//了这么长的时间和放第2步等待;没有发现
            结果=新的String(m.group(m.groupCount()));
        m.reset();    }

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 works fine. Using Emulator. Thax guys.

    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 = m.group(m.groupCount());
            m.reset();

        }

解决方案

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();

    }

这篇关于匹配没有找到第二循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆