0

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.

metropolision
  • 405
  • 4
  • 10
rdm
  • 330
  • 1
  • 4
  • 18

1 Answers1

1

matches and find begin to eat your string.

2 solutions:

or throw your test with matches()

or reset after:

 matcher.reset();

And see that: Difference between matches() and find() in Java Regex

Community
  • 1
  • 1