0

I have 2 java programs that does a pattern matching,

Program - 1

    public class test {

        public static void main(String[] args) {
            Pattern p = Pattern.compile("\\d*");
            Matcher m = p.matcher("ab34ef");
            boolean b = false;
            while (b = m.find()){
                System.out.println(m.start());
                System.out.println(m.group());
            }
        }
    }

Output:

    0
    1
    2
    34
    4
    5
    6

Program - 2

public class test {

    public static void main(String[] args) {
        Pattern p = Pattern.compile("Dog");
        Matcher m = p.matcher("This is a Dog and Dog name is Tommy");
        boolean b = false;
        while (b = m.find()){
            System.out.println(m.start());
            System.out.println(m.group());
        }
    }
}

Output-

    10
    Dog
    18
    Dog

Can someone explain how the regex works in both these cases.. Why in program-1 the matching is started from byte 0 and there on...whereas in program-2 the matching is matched on the whole string?

DT7
  • 1,615
  • 14
  • 26
user1050619
  • 19,822
  • 85
  • 237
  • 413

2 Answers2

2

\\d* means a string containing 0 or more numbers.

You get results for 0 long (empty) number strings...

You should probably try \\d+ to find strings of numbers that are at least 1 long

Recommended reading

Community
  • 1
  • 1
ppeterka
  • 20,583
  • 6
  • 63
  • 78
0

Because the * in \\d* makes the digits optional. That is, that pattern matches every byte. It also specifically matches the digits when they are there, but it doesn't have to.

Dog is not an optional part of the pattern and is required for matching.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405