天天看點

JAVA正規表達式 Pattern和Matcher(三)

解釋:
Pattern pattern = Pattern.compile("^[0-9]{1,8}$");//将給定的正規表達式編譯到模式中
Matcher matcher = pattern.matcher(passWord);//建立比對給定輸入與此模式的比對器
return matcher.find();//當且僅當輸入序列的子序列比對此比對器的模式時才傳回 true

例子:
    public static void main(String[] args) {
        List<String> match = match("serfwrf1231234dfsf2445");
        System.out.println(match);
    }

    public static List<String> match(String source) {
        List<String> result = new ArrayList<String>();
        String reg = "\\d*[^\\D*]";
        Matcher m = Pattern.compile(reg).matcher(source);
        while (m.find()) {
            String r = m.group(0);
            result.add(r);
        }
        return result;
    }

結果:
[1231234, 2445]