问题
I used perl, unix and java regular expression lot of time, but I'm surprised in java about that:
"help".matches("^h")
is false!!
From java documentation: http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#matches-java.lang.String-)
"true if, and only if, this string matches the given regular expression"
"help".matches("^h.*")
or
"help".matches("^h.*$")
return of course true.
It's surprising only me?
回答1:
"help"
does not macth "^h"
. Only the first letter in help
matches "^h"
回答2:
Java is a bit more strict than say perl or ruby. It's trying to match the entire string and "help" has an extra elp at the end that /^h/ won't match on.
From the docs:
Tells whether or not this string matches the given regular expression.
Not a substring, the whole string.
来源:https://stackoverflow.com/questions/7418924/string-matches-regexp