Why does clojure's re-matcher
fail for the same string-pattern combo that re-find
works for?
Regex Pattern: #"\[(?<level>[A-Z]*)\]:\s(?<msg>.*)"
Example String: [WARNING]: \tTimezone not set \r\n
Below is an example on the console with above pattern & string and another string that works with both re-find
and re-matcher
(with the same pattern).
user=> (def s1 "[ERROR]: This is an error.")
#'user/s1
user=> (def s2 "[WARNING]: \tTimezone not set \r\n")
#'user/s2
user=> (def rx #"\[(?<level>[A-Z]*)\]:\s(?<msg>.*)")
#'user/rx
user=> (re-find rx s1)
["[ERROR]: This is an error." "ERROR" "This is an error."]
user=> (re-find rx s2)
["[WARNING]: \tTimezone not set " "WARNING" " \tTimezone not set "]
user=> (def m1 (re-matcher rx s1))
#'user/m1
user=> (def m2 (re-matcher rx s2))
#'user/m2
user=> (.matches m1)
true
user=> (.matches m2)
false
As you can see from the code snippet, re-find
works on string s2
, however re-matcher
's matches
method returns false
for the same string-pattern combination.
I read that re-find
uses the Matcher
methods behind the scenes (ref), so what am I missing here?