by compiling following:
system.out.println(pattern.matches(".?(\\d)$","3"));
it returns true because before 3
there nothing , ?
check 1 or zero. 3 first character of input starts @ 0 , end @ 1. how can jvm recognize there nothing before 3. example following.
system.out.println(pattern.matches(".*","hello");
it returns true last character gets matched "nothing". there should not "nothing" character @ beginning of string, @ end of right?
- this not jvm. java regular expressions.
- the regular expression ".*" means "match 0 or more characters". it's easy satisfy this, since blank string has 0 characters, , therefore satisfies this. whether java regular expressions choose lazy , match empty string, or greedy , match entire string depends on implementation of java regular expressions. if read excellent writeup (http://docs.oracle.com/javase/tutorial/essential/regex/quant.html)
you can see patterns ".*" in java considered "reluctant" quantifiers , prefer take little possible. based on information in writeup, can see pattern ".{0,}" greedy version of same expression. perhaps you'd use instead if problem you.
Comments
Post a Comment