Simple matching question

View: New views
11 Messages — Rating Filter:   Alert me  

Simple matching question

by Cees de Groot-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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?

TIA,

Cees


Re: Simple matching question

by Randall Schulz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: Simple matching question

by Naftoli Gugenheim :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

See the recent thread about regexes in pattern matching. But you could also do
 case x if x.startsWith("aaa") =>
etc.


On Fri, Jul 3, 2009 at 4: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


Re: Simple matching question

by Daniel Sobral :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

Re: Simple matching question

by Ricky Clarkson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/3 Daniel Sobral <dcsobral@...>:
> Funny that I don't see Cees original message.

Me neither.

Re: Simple matching question

by Randall Schulz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel, Ricky,

On Friday July 3 2009, Daniel Sobral wrote:
> Funny that I don't see Cees original message.

Here it is, in first-level-quoted form:

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?
>
> TIA,
>
> Cees


Randall Schulz

Re: Simple matching question

by Daniel Sobral :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Well, I saw the quote, but I didn't get that e-mail itself.

On Mon, Jul 6, 2009 at 10:00 AM, Randall R Schulz <rschulz@...> wrote:
Daniel, Ricky,

On Friday July 3 2009, Daniel Sobral wrote:
> Funny that I don't see Cees original message.

Here it is, in first-level-quoted form:

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?
>
> 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.

Re: Simple matching question

by Florian Hars-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel Sobral schrieb:
> Well, I saw the quote, but I didn't get that e-mail itself.

That was probably your spam filter. Gmane seems to be listed on several
black and white lists, so what your filter makes of this can be pretty
arbitrary, this is what my spamasassin says:

X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,
        RCVD_IN_BL_SPAMCOP_NET,RCVD_IN_DNSWL_MED,RCVD_IN_SORBS_WEB,RCVD_NUMERIC_HELO

- Florian

Re: Simple matching question

by Randall Schulz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Monday July 6 2009, Florian Hars wrote:
> Daniel Sobral schrieb:
> > Well, I saw the quote, but I didn't get that e-mail itself.
>
> That was probably your spam filter. Gmane seems to be listed on
> several black and white lists, so what your filter makes of this can
> be pretty arbitrary, this is what my spamasassin says:

Never let a spam filter decide what you receive, only its default
dispensation when it reaches your MUA. I get enough false positives to
make this policy a necessity. (Some false negatives, too...)


> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,
> RCVD_IN_BL_SPAMCOP_NET,RCVD_IN_DNSWL_MED,RCVD_IN_SORBS_WEB,RCVD_NUME
>RIC_HELO
>
> - Florian


Randall Schulz

Re: Simple matching question

by Cees de Groot-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, 03 Jul 2009 18:44:24 -0300, Daniel Sobral wrote:
> Anyway, "startsWith" is a method of both String and RichString, so I'm
> not sure how you weren't able to find it.[...]

I'm a Scala n00b, so even if I found it, getting the hang of this
matching business ain't easy (for my old and rotting brains...).

Thanks for the example, most appreciated!


Re: Simple matching question

by Daniel Sobral :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yeah, well, all that is fine, but that e-mail is not in my spam box.

On Mon, Jul 6, 2009 at 12:19 PM, Randall R Schulz <rschulz@...> wrote:
On Monday July 6 2009, Florian Hars wrote:
> Daniel Sobral schrieb:
> > Well, I saw the quote, but I didn't get that e-mail itself.
>
> That was probably your spam filter. Gmane seems to be listed on
> several black and white lists, so what your filter makes of this can
> be pretty arbitrary, this is what my spamasassin says:

Never let a spam filter decide what you receive, only its default
dispensation when it reaches your MUA. I get enough false positives to
make this policy a necessity. (Some false negatives, too...)


> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,
>       RCVD_IN_BL_SPAMCOP_NET,RCVD_IN_DNSWL_MED,RCVD_IN_SORBS_WEB,RCVD_NUME
>RIC_HELO
>
> - Florian


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.