I'm studying for my scjp 1.5, and pattern matching comes out unfortunately.
I do have some questions :
Is this true (My understanding)? : Find me groups of 1 of integer, and when you found one, print me out the index of the found integer based on the String and the group of 1 integer.
the output in compiler :
However, if i add a * at the back of \\d, which is :
the compiler prints out :
Why is this so? I thought what i'm telling the compiler is "Hey compiler, find me groups which start with a digit, while you're on it, take anymore integers behind the first one and form that into a group. Then move on the next token."
I really hope someone can give me some feedback. My exam is like less than 2 weeks away, and i'm really new on this chapter. My lecturer said that we weren't taught this before, but he skipped it.. I mean, wth. lol
If you are really concerned about revealing your real name, you can always put a fake name as long as it sounds like a real name.
I did some looking into your first question:
When you do Pattern.compile("\\d*"); it's saying find me a match with 0 or more digits. What you are really looking for is Pattern.compile("\\d+"); find me a match with 1 or more digits. If you replace * with a + that should do it.
As to why the * prints 01234456 is because when you have 0 or more digits, pretty much everything makes m.find() return true. At least that's what I think. So it goes by every charcter and return true:
a:true but empty group b:true but empty group 3:true group:34 e:true but empty group f:true but empty group
as to the extra 6 in the end, it puzzles me a bit. Seems like there is a hidden character of some sort that is always attached to the end and always makes m.find() return true even though there is no group printed. When you use * or ?.