Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why with scala, using the same regular expression, using 2 different matching methods lead to 2 different results?

Tags:

regex

scala

Why there is a match here:

scala> """\bdog\b""".r
res65: scala.util.matching.Regex = \bdog\b
scala> res65.findFirstIn(" The dog plays in the yard")
res66: Option[String] = Some(dog)

But not here:

scala> "The dog plays in the yard".matches("""\bdog\b""")
res67: Boolean = false

?

like image 844
Jérôme Avatar asked Dec 12 '22 09:12

Jérôme


1 Answers

In the second case, the whole stirng has to match the regex, in the first case, any part of the string can match. Compare the second case to this:

"The dog plays in the yard".matches(""".*\bdog\b.*""")
like image 164
Kim Stebel Avatar answered Dec 14 '22 22:12

Kim Stebel