-2

Could anyone help me - I am taking preparation for my OCJP 6 exam. Here is an example:

import java.util.regex.*;

class Regex2 {
    public static void main(String[] args) {
        Pattern p = Pattern.compile(args[0]);
        Matcher m = p.matcher(args[1]);
        boolean b = false;
        while(b = m.find()) {
            System.out.print(m.start() + m.group());
        }
    }
}

compile: javac Regex2.java

run: java Regex2 "\d*" ab34ef

output: 01234456

The last index is 5(f) so why in output there is 6?

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
  • Before asking question try to search for similar questions. Simple [search for `ab34ef`](http://stackoverflow.com/search?tab=votes&q=ab34ef) gives lot of questions about this subject. – Pshemo May 04 '14 at 16:10
  • Sorry for that I haven't found it – user3601696 May 04 '14 at 16:19

1 Answers1

0

It is because \d* can match the empty string at the end of the subject string.

The last result is:

offset: 6
match: ''

To avoid this kind of results, use \d+ (one or more times) instead of \d* (zero or more times).

Note: for the same reason you obtain a match (for the empty string) at offset 0,1,4,5

   a   b   3   4   e   f   \0
 ^   ^   ^       ^   ^   ^
 |   |   |       |   |   |
 0   1   2       4   5   6      offset
 |   |   |       |   |   |
 +---+---|-------+---+---+----> match the empty string
         +--------------------> match '34'
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125