1

Why is the matching behavior different for the same matcher?

val str = "project git commit: da2837ec0a" val Expr = "([a-f0-9]{10})$".r scala> str match { case Pattern(c) => c; case _ => "no match" } res30: String = no match scala> (Pattern findFirstIn str).get res31: String = da2837ec0a 

1 Answer 1

10

When you use regular expressions with pattern matching, you must match the entire string. If this behaviour is not desired, you can make it an unanchored regular expression thus: val Pattern = "whatever".r.unanchored

In contrast, findFirstIn looks for a match anywhere within the string – and so doesn't require unanchored for this to match in your example.

This is documented in the API reference which is often helpful for questions like these.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.