title: Scala list of regular expressions with pattern matching
body: <p>
  I have some regular expressions that are stored in variables and a matching operation that returns a string depending on the pattern matched (displayed below). I'd like to convert these variables into an Array or a List of Regex, so they can be indexed and I can get the correspond groups and return the appropriate result.</p>
  
  <p><strong>Existing code:</strong> </p>
  
  <pre><code>def parseString(str : String) : String = {
  
      val ptrn2 = """/foo/bar/find/(apple|orange)/(\w+)$""".r
      val ptrn3 = """/foo/bar/(fruit|vegetable)/(banana|carrot)/(\w+)$""".r
      val ptrn4 = """/foo/bar/(volume|size)/(\d+)/(\d+)/(\d+)$""".r
      // more variables
  
      val response =  str match {
          case ptrn2(a,b) =&gt; "/foo/bar/"+ a +"/{id}"
          case ptrn3(a,b,c) =&gt; "/foo/bar/"+ a +"/" + b + "/{ref}"
          case ptrn4(a,b,c,d) =&gt; "/foo/bar/"+ a +"/" + (b.toInt*c.toInt*d.toInt)
          // more case statements
          case _ =&gt; str
       }
       return response 
  }
  </code></pre>
  
  <p>I tried to use the syntax below to access a specific index, passing variables to get the groups, but this is incorrect. What's wrong with that?</p>
  
  <pre><code> val prtn : List[Regex] = List(new Regex("regex here"),
    new Regex("regex2 here"),
    new Regex("regex3 here"))
  
   val response =  str match {
          case ptrn(0)(a,b) =&gt; "/foo/bar/"+ a +"/{id}"
          case ptrn(1)(a,b,c) =&gt; "/foo/bar/"+ a +"/" + b + "/{ref}"
          case ptrn(2)(a,b,c,d) =&gt; "/foo/bar/"+ a +"/" + (b.toInt*c.toInt*d.toInt)
          case _ =&gt; str
   }
  </code></pre>
  
  <p>There must be a way to access this via Arrays/List in the match block or it would be even better if a Map returned the appropriate result. Does anyone have any idea how to solve this in Scala?</p>