正则表达式跳过第一场比赛

时间:2015-06-27 15:14:24

标签: android regex

正则表达式 .*([0-9]{3}\\.[0-9]{2}).* 在"一些短句111.01"中找到一个匹配,但它未能匹配第一次出现" 111.01"在"一些短句111.01& 222.02" 我尝试了懒惰量词.*([0-9]{3}\\.[0-9]{2})?.*.*([0-9]{3}\\.[0-9]{2}).*?无济于事。 请帮助,我需要两次出现,这是我的代码。

谢谢

Pattern myPattern = Pattern.compile(".*([0-9]{3}\\.[0-9]{2}).*");
Matcher m = myPattern.matcher(mystring);
    while (m.find()) {
        String found = m.group(1);
    }

2 个答案:

答案 0 :(得分:0)

领先和尾随"。*"使您在一场比赛中匹配整个字符串。在你的情况下,所有惰性量词都是控制你得到主题中的第一个而不是最后一个。

答案 1 :(得分:0)

你需要删除"。*" s。试试这个:

        String mystring = "some short sentence 111.01 & 222.02 ";
        Pattern myPattern = Pattern.compile("([0-9]{3}\\.[0-9]{2})");
        Matcher m = myPattern.matcher(mystring);
        while(m.find()) {
               System.out.println("Found value: " + m.group(1) );
        }

输出:

Found value: 111.01
Found value: 222.02
相关问题