为什么scala使用相同的正则表达式,使用2种不同的匹配方法导致2种不同的结果?

Jér*_*ôme 4 regex scala

为什么这里有匹配:

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)
Run Code Online (Sandbox Code Playgroud)

但不是这里:

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

Kim*_*bel 8

在第二种情况下,整个搅拌必须匹配正则表达式,在第一种情况下,字符串的任何部分都可以匹配.比较第二种情况:

"The dog plays in the yard".matches(""".*\bdog\b.*""")
Run Code Online (Sandbox Code Playgroud)


Tim*_*ker 5

你比较findFirstInmatches,当然字符串中包含,但不匹配 \bdog\b.