tribbloid
tribbloid

Reputation: 3838

Scala regex pattern matching doesn't work

val login = "login user=(.*), token=(.*)".r

"login user=SapHana_dummy token=dummy" match {
  case login(user, token) =>
    println("success")
}

This code always throws MatchError. Instead of printing "success" as intended. Why?

Upvotes: 0

Views: 134

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

Because your pattern expects a comma, here:

login user=(.*), token=(.*)
               ^

Which is not in the input text.

Also, to minimize backtracking, I'd use an ungreedy quantifier here:

login user=(.*?) token=(.*)

Upvotes: 3

Related Questions