Why does not work multiple selection?

时间:2015-12-13 00:18:13

标签: java regex

Why does not work multiple selection?

I need to get all numbers from a string.

        Pattern pattern = Pattern.compile("(([1-9]\\d*(\\.|\\,)\\d*)|0{1}(\\.|\\,)\\d*|(\\.|\\,)\\d+|([1-9]\\d*|0{1}))"); 
        Matcher matcher = pattern.matcher(text);      
        boolean matches = matcher.matches();
        log.info("matches: {}", matches);

        if (matches) {

          log.info("matches value: {}", text);

        } else {
            while (matcher.find()) {
                String value = matcher.group();                    
                log.info("value: {}", value);                                                            
            }
     }

When text variable equals to " 111 222" it works (111,222), but if I set "111 222" (without first white space) only get (222).

I checked on regex101.com (with g flag) and pattern works as expected.

1 个答案:

答案 0 :(得分:1)

匹配并找到开始吃你的字符串。

2个解决方案:

或用match()

抛出你的测试

或在之后重置:

 matcher.reset();

看到:Difference between matches() and find() in Java Regex

相关问题