scala regex weird behavior

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 ?


aa will be matched because you use find.

(ab)*a will match a.

Maybe you want an expression like:


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.

Need Your Help

Will I be able to use Realm on Linux, with Perfect?

ios linux swift realm perfect

Perfect - - is a new web server which lets you use the same language on the server-side as you do on your the device. It will be compatible with the Linux version of Swift w...

NewRelic::Agent::ServerConnectionException: Service unavailable (503): Service Unavailable

ruby-on-rails newrelic

Configured New Relic with ruby on rails application, however newrelic_agent.log shows below error, server is on DigitalOcean.