title: Check if regex pattern matching failed
body: <p>I am working on a function that extracts a number at the beginning of the string. Given a string it should return a case class <code>Success</code> (containing the extracted number in <code>Int</code> and the rest of the string) in case it found a number at the beginning and a case Object <code>Failure</code> otherwise.</p>
  
  <p>For that, I'm using regex pattern matching to match the number and the rest of the string into two separate variable. I then convert the number to <code>Int</code> and return.</p>
  
  <p>The problem: when the string provided does not contain a number at the beginning, the pattern matching fail but I don't know how to detect that it fails, and when to return my Failure.</p>
  
  <p>Any ideas ?</p>
  
  <p>Here is my code</p>
  
  <pre><code>sealed trait Result
  case class Success(expr: String, result: Int) extends Result
  case object Failure extends Result
  
  def extractNumber(str: String) : Result = {
    val pattern = "([0-9]+)(.*)".r
    val pattern(count, rest) = str
    if (pattern match failed) //How can I do that
      return Failure
    val number = count.toInt
    return Success(rest, number)
  }
  
  extractNumber("100banana") match {
    case Success(rest, nb) =&gt; println("sucess" + nb + ";" + rest)
    case Failure =&gt; println("failure")
  }
  </code></pre>