Reputation: 409
scala> val p = "ab|ba|(ab)*a|(ba)*b".r
p: scala.util.matching.Regex = ab|ba|(ab)*a|(ba)*b
scala> val m = p.pattern.matcher _
m: java.lang.CharSequence => java.util.regex.Matcher = <function1>
scala> m("aa").matches
res9: Boolean = false
scala> p.findAllIn("aa").toList
res10: List[String] = List(a, a)
"aa" should not be matched, as per res9. But res10 says there are 2 matches. How do you explain this ?
Upvotes: 0
Views: 120
Reputation: 297185
You are asking two different things:
When you use m("aa").matches
, you are asking whether the whole string aa
corresponds to the pattern m
.
When you use p.findAllIn("aa").toList
, you are asking whether there are substrings of aa
that correspond to the pattern m
.
So, yes, there are substrings that match m
, but the full string aa
itself doesn't.
Upvotes: 4
Reputation: 33908
aa
will be matched because you use find
.
(ab)*a
will match a
.
Maybe you want an expression like:
^(?:ab|ba|(?:ab)*a|(?:ba)*b)$
Upvotes: 4