Why doesn't this regex work as expected in Java? -
trivial regex question (the answer java-specific):
"#this comment in file".matches("^#") this returns false. far can see, ^ means means , # has no special meaning, i'd translate ^# "a '#' @ beginning of string". should match. , does, in perl:
perl -e "print '#this comment'=~/^#/;" prints "1". i'm pretty sure answer java specific. please enlighten me?
thank you.
matcher.matches() checks see if entire input string matched regex.
since regex matches first character, returns false.
you'll want use matcher.find() instead.
granted, can bit tricky find concrete specification, it's there:
string.matches()defined doing same thingpattern.matches(regex, str).pattern.matches()in turn definedpattern.compile(regex).matcher(input).matches().pattern.compile()returnspattern.pattern.matcher()returnsmatcher
matcher.matches()documented (emphasis mine):attempts match entire region against pattern.
Comments
Post a Comment