I have to perform partial pattern matching, so I tested pattern matching against the following input
Pattern p = Pattern.compile("hello");
Matcher m = p.matcher("[a-z]");
Can anybody explains me why
System.out.println(m.find() || m.hitEnd());
prints true while
System.out.println(m.hitEnd());
prints false?
Look at this program:
Notice, the first call of
m.hitEnd()returnsfalse. Look at JavaDoc, it says:Here it returns
false, because it is called before the call ofm.find(), so the matcher hasn’t performed any match operations, yet. After the call ofm.find()it returnstrue(becausefind()consumes the complete input string and hits the end). The meaning of that is also explained in JavaDoc:When this returns true, it means the matcher hit the end of the input. In this case, hit means reached, not matched. (The input was completely consumed by the matcher).
EDIT
I hope it is wanted by you, that
[a-z]is the input string for your regular expressionhello, and it’s not the other way around. If you hadyour output would be