« Return to Thread: Simple matching question

Re: Simple matching question

by Daniel Sobral :: Rate this Message:

Reply to Author | View in Thread

Funny that I don't see Cees original message.
 
Anyway, "startsWith" is a method of both String and RichString, so I'm not sure how you weren't able to find it. A general matcher can be built like this:
 
class prefixMatcher(prefix: String) {
  def unapply(s: String): Boolean =
    s.startsWith(prefix)
}
 
val multipartMatcher = new prefixMatcher("multipart/")
 
for(contentType <- List("multipart/mixed", "application/soa")) contentType match {
  case multipart @ multipartMatcher() => println("Found multipart: "+multipart)
  case _ =>
}
Please note that the parenthesis after multipartMatcher are required inside the case statement.
 
On a totally unrelated note, I'm impressed how often the answer to a Scala question can be usable code without turning it into a dissertation!
 
On Fri, Jul 3, 2009 at 5:18 PM, Randall R Schulz <rschulz@...> wrote:
On Friday July 3 2009, Cees de Groot wrote:
> Hi,
>
> I'm writing some code to interpret MIME mails, and one of the things
> I want to do is create a matcher if the content-type says
> "multipart/* <garbage>" (multipart/alternative, multipart/mixed,
> multipart/related). I've scanned the Scala book up and down and
> (blame me) can't find a simple way to match on the beginning of a
> string save for the 'm','u','l',... Seq[Char] matching. Isn't there a
> simpler way?

Unless I'm misunderstanding you, a regular expression match is a good
fit for this sort of problem.


> TIA,
>
> Cees


Randall Schulz



--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.

 « Return to Thread: Simple matching question