4

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

?

1
  • 2
    Not a stupid question. In .NET for example, Regex.IsMatch() also finds substrings. In Python re.match() only anchors the search at the start but not the end of the string. Regular expressions are confusing on their own, but the different implementations vary wildly enough to make a grown man cry (unless he's got RegexBuddy, of course). Commented Sep 21, 2011 at 9:45

2 Answers 2

8

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.*""")
Sign up to request clarification or add additional context in comments.

Comments

5

You're comparing findFirstIn with matches, and of course the string contains, but doesn't match \bdog\b.

1 Comment

Then again, that definition of "match" is not the usual definition of match, as defined by libregex, sed, awk, perl, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.