1

I've got the following string:

0 days 00 hour 20 min 51 sec

I would like to extract all the numbers from it using Java's Regular Expressions:

Pattern pattern = Pattern.compile("(\\d){1,2} days (\\d){2} hour (\\d){2} min (\\d){2} sec");
Matcher m = pattern.matcher("0 days 00 hour 20 min 51 sec");

To my surprise m.group(4) returns 1 and not 51. The same goes for m.group(2) which returns 0 and not 00

I found this confusing since {n} should match exactly n occurrences of the preceding expression, or not ?

DT7
  • 1,615
  • 14
  • 26
Jonas Geiregat
  • 5,214
  • 4
  • 41
  • 60
  • The thing is that when you have repeated capture groups (e.g. `(abc)*`), only the last match will be 'saved'. This is why you need `(\\d{2})`. – Jerry Sep 18 '13 at 12:15
  • I feel this is related to http://stackoverflow.com/questions/2367381/extract-numbers-from-a-string-java – user2783484 Sep 18 '13 at 12:16

3 Answers3

4

You likely wanted to catch both digits in the same group:

"(\\d{1,2}) days (\\d{2}) hour (\\d{2}) min (\\d{2}) sec"

This is what the original expression would do.

0 days 00 hour 20 min 51 sec
  • (\d){1,2} matches 0, places it in group 1
  • (\d){2} matches 0, places it in group 2, matches 0 again, places it in group 2
  • (\d){2} matches 2, places it in group 3, matches 0, places it in group 3
  • (\d){2} matches 5, places it in group 4, matches 1, places it in group 4

You now have:

  • 0 in group 1
  • 0 in group 2
  • 0 in group 3
  • 1 in group 4
rid
  • 61,078
  • 31
  • 152
  • 193
3

The {...} structures should be inside the (...) parenthesis, e.g.

Pattern.compile("(\\d{1,2}) days (\\d{2}) hour (\\d{2}) min (\\d{2}) sec");
rolfl
  • 17,539
  • 7
  • 42
  • 76
0

It should be (\\d{1,2}) You're leaving the repetition operator out of the group.

Yasen
  • 1,663
  • 10
  • 17