mardi 8 mars 2016

Why matcher loops forever when using find() and "" as a pattern?

When given empty string as a pattern the following code loops forever. Why this code loops forever? Did I misuse find(int) method?

        Pattern pattern = Pattern.compile("");
        Matcher matcher = pattern.matcher("some text");    

        int pos = 0;
        int i = 0;
        while (matcher.find(pos))
        {
            int start = matcher.start();
            int end = matcher.end();
            pos = end;
            System.out.format("%d", i++);
        }

Aucun commentaire:

Enregistrer un commentaire